当我尝试使用`.read()`方法读取JSON文件时,出现了OSError:[Errno 22]。

9

我只是想在Python中读取我的JSON文件。当我这样做时,我在正确的文件夹中; 我在下载文件夹中,我的文件名为“ Books_5.json” 。但是,当我尝试使用 .read() 函数时,出现了错误。

OSError: [Errno 22] Invalid argument

这是我的代码:
import json
config = json.loads(open('Books_5.json').read())

这也会引发相同的错误:
books = open('Books_5.json').read()

如果有帮助的话,这是我的数据样本的一小部分:

{"reviewerID": "A10000012B7CGYKOMPQ4L", "asin": "000100039X", "reviewerName": "Adam", "helpful": [0, 0], "reviewText": "Spiritually and mentally inspiring! A book that allows you to question your morals and will help you discover who you really are!", "overall": 5.0, "summary": "Wonderful!", "unixReviewTime": 1355616000, "reviewTime": "12 16, 2012"}
{"reviewerID": "A2S166WSCFIFP5", "asin": "000100039X", "reviewerName": "adead_poet@hotmail.com \"adead_poet@hotmail.com\"", "helpful": [0, 2], "reviewText": "This is one my must have books. It is a masterpiece of spirituality. I'll be the first to admit, its literary quality isn't much. It is rather simplistically written, but the message behind it is so powerful that you have to read it. It will take you to enlightenment.", "overall": 5.0, "summary": "close to god", "unixReviewTime": 1071100800, "reviewTime": "12 11, 2003"}

我在MacOSX上使用Python 3.6。

1
@DanilaGanchar,让我补充一下这个信息,谢谢你的提醒。 - user45254
@user45254 尝试使用 codecsimport codecs f = codecs.open(filename, encoding="utf-8") ... - Danila Ganchar
@DanilaGanchar 是的。而且,如果文件未找到,我会得到不同的错误(文件未找到错误)。 - user45254
@user45254,我没有MacOSX无法重复此问题。尝试在文件中设置空的JSON - {}。并检查它 print(open('Books_5.json').read()) - Danila Ganchar
1
@DanilaGanchar 是的,实际上那个可以毫无问题地运行。 - user45254
显示剩余7条评论
3个回答

15

看起来这是一种当文件过大时出现的bug(我的文件约为10GB)。一旦我使用 split 按200k行拆分文件,.read() 的错误就消失了。即使文件不是严格的json格式也是如此。


2
我在打开一个8GB的JSON文件时遇到了同样的问题。您能否分享一下如何将文件分成200k行?我不太明白如何使用“split”将JSON文件拆分成较小的文件。 - Box Box Box Box
遇到了与Mac版本10.13.3相同的问题,文件大小约为10GB。 - Daniel
1
尝试使用ijson将JSON作为流加载的备选方案。 - Alon Eirew

0

你的代码看起来没问题,只是你的JSON数据格式可能有误。尝试以下方法。就像其他人建议的一样,它应该以 [{},{},...] 的形式出现。

[{"reviewerID": "A10000012B7CGYKOMPQ4L", "asin": "000100039X", 
"reviewerName": "Adam", "helpful": [0, 0], "reviewText": "Spiritually and 
mentally inspiring! A book that allows you to question your morals and will 
help you discover who you really are!", "overall": 5.0, "summary": 
"Wonderful!", "unixReviewTime": 1355616000, "reviewTime": "12 16, 2012"},
{"reviewerID": "A2S166WSCFIFP5", "asin": "000100039X", "reviewerName": 
"adead_poet@hotmail.com \"adead_poet@hotmail.com\"", "helpful": [0, 2], 
"reviewText": "This is one my must have books. It is a masterpiece of 
spirituality. I'll be the first to admit, its literary quality isn't much. 
It is rather simplistically written, but the message behind it is so 
powerful that you have to read it. It will take you to enlightenment.", 
"overall": 5.0, "summary": "close to god", "unixReviewTime": 1071100800, 
"reviewTime": "12 11, 2003"}]

你的代码和这个数据在Windows 7和Python 2.7上对我有效。虽然与你的设置不同,但应该仍然可以使用。


是的,那个方法可行。你有没有快速转换数据的方法?这是一个非常大的文件。我想在每个 } 后面插入一个逗号,除了最后一个之外,然后在开头添加 [,在结尾添加 ]。如果你能够立刻想到 Pythonic 的方法就告诉我吧,否则我会自己解决的。感谢你的帮助。 - user45254
它们是否像你的示例中一样分别在不同的行上?如果是的话,只需按行读取文件(参见 readline())并将每行追加到一个列表中。 - Anddrrw
我想是这样,稍后我会测试一下。谢谢你的帮助。 - user45254
那也不起作用。但事实证明问题是文件太大了。请看下面的答案。 - user45254
https://meta.stackoverflow.com/questions/277923/are-your-code-works-fine-for-me-answers-acceptable - Jean-François Fabre

-1
为了读取 json 文件,您可以使用以下示例:
with open('your_data.json') as data_file:    
    data = json.load(data_file)

print(data)
print(data[0]['your_key']) # get value via key.

同时尝试将您的json对象转换为列表

[
  {'reviewerID': "A10000012B7CGYKOMPQ4L", ....},
  {'asin': '000100039X', .....}
]

1
信不信由你,我这里还是出现了同样的错误:OSError: [Errno 22] Invalid argument - user45254
也许 @DanilaGanchar 是对的——如果数据形式为 {} {} {} 而不是 [{},{},{}],那么这就是导致错误的原因吗? - user45254
是的,您需要将您的 json 对象转换成一个列表。然后一切应该都能正常工作。 - user5639219

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接