2017-05-01 31 views
2

我只是想在Python中閱讀我的json文件。當我這樣做時,我在正確的文件夾中;我在下載中,我的文件被稱爲'Books_5.json'。然而,當我嘗試使用.read()函數,我得到的錯誤OSError:[Errno 22]當我嘗試.read()一個json文件

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": "[email protected] \"[email protected]\"", "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"} 

我使用Python 3.6 MacOSX上

+0

您是否嘗試過使用完整路徑?例如'開(R'C:\用戶\ your_name \下載\ Books_5.json')'?如果完整路徑有效,但相對路徑不適用,則當前工作目錄不在您認爲的位置。 – CoryKramer

+0

哪個版本的Python?哪個OS? –

+1

@DanilaGanchar讓我添加該信息感謝提醒 – user45254

回答

4

看起來,這是某種在文件過大時發生的錯誤(我的文件是〜10GB)。一旦我使用split將文件分割爲200 k行,則.read()錯誤消失。即使文件不是嚴格的json格式,情況也是如此。

0

爲了閱讀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', .....} 
] 
+0

相信與否,我仍然在這裏有同樣的錯誤:'OSError:[Errno 22]無效參數' – user45254

+0

也許@DanilaGanchar是正確的 - 如果數據是形式爲{} {} {}',而不是'[{},{},{}]',這是否會導致錯誤? – user45254

+0

是的,你需要將你的'json'對象轉換成一個列表。然後一切都應該正常工作。 –

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": 
"[email protected] \"[email protected]\"", "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。不同於你的設置,但應該還可以。

+0

是的,確實有效。你碰巧有任何快速的方式來轉換數據?這是一個非常大的文件。我正在考慮在每個'}'之後插入一個逗號,除了最後一個',然後在開頭和尾部添加[。什麼是pythonic方式,如果它不在你的頭頂?否則,我只是想出來。感謝您的幫助。 – user45254

+0

它們是否都像你的例子中的單獨行?如果是這樣,只需一次讀取一行文件(參見['readline()'](https://docs.python.org/2/tutorial/inputoutput.html))並將每行添加到列表中。 – Anddrrw

+0

我這麼認爲,我會稍後再測試。謝謝你的幫助。 – user45254

相關問題