问题:使用Python将JSON数据漂亮地打印到文件中
用于类的项目涉及解析Twitter JSON数据。我正在获取数据并将其设置为文件没有太大的麻烦,但是它们全部集中在一行中。这对我要进行的数据操作很好,但是文件很难读取,而且我无法很好地对其进行检查,这使得为数据操作编写代码非常困难。
有谁知道如何在Python中执行此操作(即不使用命令行工具,但我无法使用该工具)?到目前为止,这是我的代码:
header, output = client.request(twitterRequest, method="GET", body=None,
headers=None, force_auth_header=True)
# now write output to a file
twitterDataFile = open("twitterData.json", "wb")
# magic happens here to make it pretty-printed
twitterDataFile.write(output)
twitterDataFile.close()
请注意,我很高兴有人向我指向simplejson文档等,但是正如我已经说过的那样,我已经研究过了并继续需要帮助。一个真正有用的答复将比那里的示例更加详细和解释。谢谢
另外:
在Windows命令行中尝试此操作:
more twitterData.json | python -mjson.tool > twitterData-pretty.json
结果:
Invalid control character at: line 1 column 65535 (char 65535)
我会给您我正在使用的数据,但是它非常大,您已经看到了我用来制作文件的代码。
A project for class involves parsing Twitter JSON data. I’m getting the data and setting it to the file without much trouble, but it’s all in one line. This is fine for the data manipulation I’m trying to do, but the file is ridiculously hard to read and I can’t examine it very well, making the code writing for the data manipulation part very difficult.
Does anyone know how to do that from within Python (i.e. not using the command line tool, which I can’t get to work)? Here’s my code so far:
header, output = client.request(twitterRequest, method="GET", body=None,
headers=None, force_auth_header=True)
# now write output to a file
twitterDataFile = open("twitterData.json", "wb")
# magic happens here to make it pretty-printed
twitterDataFile.write(output)
twitterDataFile.close()
Note I appreciate people pointing me to simplejson documentation and such, but as I have stated, I have already looked at that and continue to need assistance. A truly helpful reply will be more detailed and explanatory than the examples found there. Thanks
Also:
Trying this in the windows command line:
more twitterData.json | python -mjson.tool > twitterData-pretty.json
results in this:
Invalid control character at: line 1 column 65535 (char 65535)
I’d give you the data I’m using, but it’s very large and you’ve already seen the code I used to make the file.
回答 0
您应该使用可选参数indent
。
header, output = client.request(twitterRequest, method="GET", body=None,
headers=None, force_auth_header=True)
# now write output to a file
twitterDataFile = open("twitterData.json", "w")
# magic happens here to make it pretty-printed
twitterDataFile.write(simplejson.dumps(simplejson.loads(output), indent=4, sort_keys=True))
twitterDataFile.close()
You should use the optional argument indent
.
header, output = client.request(twitterRequest, method="GET", body=None,
headers=None, force_auth_header=True)
# now write output to a file
twitterDataFile = open("twitterData.json", "w")
# magic happens here to make it pretty-printed
twitterDataFile.write(simplejson.dumps(simplejson.loads(output), indent=4, sort_keys=True))
twitterDataFile.close()
回答 1
You can parse the JSON, then output it again with indents like this:
import json
mydata = json.loads(output)
print json.dumps(mydata, indent=4)
See http://docs.python.org/library/json.html for more info.
回答 2
import json
with open("twitterdata.json", "w") as twitter_data_file:
json.dump(output, twitter_data_file, indent=4, sort_keys=True)
你并不需要json.dumps()
,如果你不想以后解析字符串,只需简单地使用json.dump()
。它也更快。
import json
with open("twitterdata.json", "w") as twitter_data_file:
json.dump(output, twitter_data_file, indent=4, sort_keys=True)
You don’t need json.dumps()
if you don’t want to parse the string later, just simply use json.dump()
. It’s faster too.
回答 3
您可以使用python的json模块进行漂亮的打印。
>>> import json
>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
{
"4": 5,
"6": 7
}
所以,在你的情况下
>>> print json.dumps(json_output, indent=4)
You can use json module of python to pretty print.
>>> import json
>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
{
"4": 5,
"6": 7
}
So, in your case
>>> print json.dumps(json_output, indent=4)
回答 4
如果您已经具有想要格式化的JSON文件,则可以使用以下命令:
with open('twitterdata.json', 'r+') as f:
data = json.load(f)
f.seek(0)
json.dump(data, f, indent=4)
f.truncate()
If you already have existing JSON files which you want to pretty format you could use this:
with open('twitterdata.json', 'r+') as f:
data = json.load(f)
f.seek(0)
json.dump(data, f, indent=4)
f.truncate()
回答 5
如果要生成新的* .json或修改现有的josn文件,请使用“ indent”参数获取漂亮的json格式。
import json
responseData = json.loads(output)
with open('twitterData.json','w') as twitterDataFile:
json.dump(responseData, twitterDataFile, indent=4)
If you are generating new *.json or modifying existing josn file the use “indent” parameter for pretty view json format.
import json
responseData = json.loads(output)
with open('twitterData.json','w') as twitterDataFile:
json.dump(responseData, twitterDataFile, indent=4)
回答 6
import json
def writeToFile(logData, fileName, openOption="w"):
file = open(fileName, openOption)
file.write(json.dumps(json.loads(logData), indent=4))
file.close()
import json
def writeToFile(logData, fileName, openOption="w"):
file = open(fileName, openOption)
file.write(json.dumps(json.loads(logData), indent=4))
file.close()
回答 7
您可以将文件重定向到python并使用该工具打开,并使用更多内容来读取它。
示例代码将是,
cat filename.json | python -m json.tool | more
You could redirect a file to python and open using the tool and to read it use more.
The sample code will be,
cat filename.json | python -m json.tool | more