使用 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您应该使用可选参数
indent
。You should use the optional argument
indent
.您可以解析 JSON,然后使用缩进再次输出,如下所示:
请参阅 http://docs.python.org/library/ json.html 了解更多信息。
You can parse the JSON, then output it again with indents like this:
See http://docs.python.org/library/json.html for more info.
如果您不想稍后解析字符串,则不需要
json.dumps()
,只需使用json.dump()
即可。它也更快。You don't need
json.dumps()
if you don't want to parse the string later, just simply usejson.dump()
. It's faster too.您可以使用 python 的 json 模块来漂亮地打印。
所以,就你的情况而言
You can use json module of python to pretty print.
So, in your case
如果您要生成新的 *.json 或修改现有的 josn 文件,请使用“indent”参数以获得漂亮的视图 json 格式。
If you are generating new *.json or modifying existing josn file the use "indent" parameter for pretty view json format.
如果您已经有现有的 JSON 文件,并且想要对其进行漂亮的格式化,则可以使用以下命令:
If you already have existing JSON files which you want to pretty format you could use this:
您可以将文件重定向到 python 并使用该工具打开并使用 more 来读取它。
示例代码将是,
You could redirect a file to python and open using the tool and to read it use more.
The sample code will be,