Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't write long JSON output to text file

Tags:

python

json

I have a long string (8,315 characters) worth of JSON, but I can't seem to write it to a .txt file using Python without it being truncated.

I write the JSON to a text file and then upload it via FTP, but both the .txt file on my system and the .txt file on the FTP server are truncated.

Here's the code:

# Upload the results
host = ftputil.FTPHost('ftp.website.com', 'username', 'password')
jsonOutput = json.dumps(full_json)
f = open('C:/Comparison.txt', 'w')
f.write(jsonOutput)
host.upload('C:/Comparison.txt', '/public_html/Comparison.txt')
f.close()
print jsonOutput

The JSON output in the console is valid and whole, but it is truncated in the .txt file that is written (and then the .txt file after it is uploaded).

Most of the time, the output will end at http://www.digikey.com/product-detail/en/A000073/1050-10 when the full URL is actually http://www.digikey.com/product-detail/en/A000073/1050-1041-ND/3476357 (and then of course, it cuts off the rest of the JSON)

I'm not sure if this makes any difference, but I also tried f.write(re.escape(jsonOutput) with the same results.

Can anyone help with this?

like image 623
Mike Buss Avatar asked Aug 20 '12 00:08

Mike Buss


1 Answers

with open('C:/Comparison.txt', 'w') as f:
  json.dump(full_json, f)
like image 179
Ignacio Vazquez-Abrams Avatar answered Oct 28 '22 23:10

Ignacio Vazquez-Abrams