使用Python将文件打印到文件中

时间:2022-10-23 21:45:15

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.

类的项目涉及解析Twitter JSON数据。我正在获取数据并将其设置到文件中没有太多麻烦,但它只是在一行中。这对我正在尝试的数据操作很好,但是文件非常难以阅读,我无法很好地检查它,使得为数据操作部分编写代码非常困难。

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:

有没有人知道如何从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()

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

请注意,我感谢有人指着simplejson文档等,但正如我所说,我已经看过并继续需要帮助。一个真正有用的回复将比那里的例子更详细和更具说明性。谢谢

Also: Trying this in the windows command line:

另外:在Windows命令行中尝试这个:

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.

我会给你我正在使用的数据,但它非常大,你已经看到了我用来制作文件的代码。

5 个解决方案

#1


35  

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()

#2


32  

You can parse the JSON, then output it again with indents like this:

你可以解析JSON,然后用这样的缩进再次输出它:

import json
mydata = json.loads(output)
print json.dumps(mydata, indent=4)

See http://docs.python.org/library/json.html for more info.

有关详细信息,请参阅http://docs.python.org/library/json.html。

#3


11  

You can use json module of python to pretty print.

您可以使用python的json模块进行打印。

>>> 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


10  

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.

如果您不想稍后解析字符串,则不需要json.dumps(),只需使用json.dump()即可。它也更快。

#5


-3  

You could redirect a file to python and open using the tool and to read it use more.

您可以将文件重定向到python并使用该工具打开并阅读它使用更多。

The sample code will be,

示例代码将是,

cat filename.json | python -m json.tool | more

#1


35  

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()

#2


32  

You can parse the JSON, then output it again with indents like this:

你可以解析JSON,然后用这样的缩进再次输出它:

import json
mydata = json.loads(output)
print json.dumps(mydata, indent=4)

See http://docs.python.org/library/json.html for more info.

有关详细信息,请参阅http://docs.python.org/library/json.html。

#3


11  

You can use json module of python to pretty print.

您可以使用python的json模块进行打印。

>>> 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


10  

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.

如果您不想稍后解析字符串,则不需要json.dumps(),只需使用json.dump()即可。它也更快。

#5


-3  

You could redirect a file to python and open using the tool and to read it use more.

您可以将文件重定向到python并使用该工具打开并阅读它使用更多。

The sample code will be,

示例代码将是,

cat filename.json | python -m json.tool | more