简体   繁体   English

无法将长JSON输出写入文本文件

[英]Can't write long JSON output to text file

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. 我有一个长字符串(8,315个字符)的JSON,但我似乎无法使用Python将其写入.txt文件而不会被截断。

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. 我将JSON写入文本文件,然后通过FTP上传,但我的系统上的.txt文件和FTP服务器上的.txt文件都被截断了。

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). 控制台中的JSON输出有效且完整,但在写入的.txt文件中被截断(然后在上载后的.txt文件中)。

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) 大多数情况下,当完整的URL实际上是http://www.digikey.com/product-detail/en/A000073/1050-1041-ND/3476357时,输出将在http://www.digikey.com/product-detail/en/A000073/1050-10结束http://www.digikey.com/product-detail/en/A000073/1050-1041-ND/3476357 (当然,它会切断其余的JSON)

I'm not sure if this makes any difference, but I also tried f.write(re.escape(jsonOutput) with the same results. 我不确定这是否有任何区别,但我也尝试了f.write(re.escape(jsonOutput) ,结果相同。

Can anyone help with this? 有人能帮忙吗?

with open('C:/Comparison.txt', 'w') as f:
  json.dump(full_json, f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM