OSError: [Errno 22] when I try to .read() a json f

2020-07-11 09:32发布

问题:

I am simply trying to read my json file in Python. I am in the correct folder when I do so; I am in Downloads, and my file is called 'Books_5.json'. However, when I try to use the .read() function, I get the error

OSError: [Errno 22] Invalid argument

This is my code:

import json
config = json.loads(open('Books_5.json').read())

This also raises the same error:

books = open('Books_5.json').read()

If it helps, this is a small snippet of what my data looks like:

{"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"}

I'm using Python 3.6 on MacOSX

回答1:

It appears that this is some kind of bug that occurs when the file is too large (my file was ~10GB). Once I use split to break up the file by 200 k lines, the .read() error goes away. This is true even if the file is not in strict json format.



回答2:

Your code looks fine, it just looks like your json data is formatted incorrectly. Try the following. As others have suggested, it should be in the form [{},{},...].

[{"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"}]

Your code and this data worked for me on Windows 7 and python 2.7. Different than your setup, but should still be ok.



回答3:

In order to read json file, you can use next example:

with open('your_data.json') as data_file:    
    data = json.load(data_file)

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

and also try to convert your json objects into a list

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