我對Python真的很陌生,在運行我的代碼時遇到了一些錯誤。python解析函數錯誤
我有這個亞馬遜數據集被格式化爲JSON文件 (請參閱下面的json格式)。
{
"reviewerID": "A2SUAM1J3GNN3B",
"asin": "0000013714",
"reviewerName": "J. McDonald",
"helpful": [2, 3],
"reviewText": "I bought this for my husband who plays the piano. He is
having a wonderful time playing these old hymns. The music is at times
hard to read because we think the book was published for singing from more
than playing from. Great purchase though!",
"overall": 5.0,
"summary": "Heavenly Highway Hymns",
"unixReviewTime": 1252800000,
"reviewTime": "09 13, 2009"
}
我使用由數據發送方,其中所述JSON文件轉換成以上的嚴格JSON'文件所提供的命令(原JSON文件不是基於所述數據的發送者嚴格JSON)。
他們所提供的命令如下:
import json
import gzip
def parse(path):
g = gzip.open(path, 'r')
for l in g:
yield json.dumps(eval(l))
f = open("output.strict", 'w')
for l in parse("reviews_Video_Games.json.gz"):
f.write(l + '\n')
我只是改變了路徑,將加上引號的JSON文件(例如,「C的目錄:\用戶\菊花\研究\學習\亞馬遜\ reviews_Video_Games.json.gz「)
例如,我跑的代碼如下所示:
import json
import gzip
def parse(C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz):
g = gzip.open(C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz, 'r')
for l in g:
yield json.dumps(eval(l))
f = open("output.strict", 'w')
for l in parse("reviews_Video_Games.json.gz"):
f.write(l + '\n')
不過,我得到以下錯誤:
C:\Users\daisy\AppData\Local\Programs\Python\Python36-32>python C:\Users\daisy\AppData\Local\Programs\Python\strict_json.py
File "C:\Users\daisy\AppData\Local\Programs\Python\strict_json.py", line 4
def parse("C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz"):
^
SyntaxError: invalid syntax
你知道語法有什麼問題嗎?
原始代碼再次由數據發送者給出,所以我確信代碼是正確的。當我將「路徑」更改爲我的文件目錄時,我認爲我做錯了什麼。
謝謝。
'高清解析(C:\用戶\菊花\研究\學習\亞馬遜\ reviews_Video_Games.json.gz):'? –
該功能是爲了按原樣使用。 'path'是您在調用函數時提供的參數,而不是您在函數定義本身中應該更改的內容。 – jasonharper
這不是它的工作原理。當你調用函數時,你必須替換這個值,例如''in l in parse(r「C:\ Users \ daisy \ Research \ study \ Amazon \ reviews_Video_Games.json.gz」):'另外,別忘了'「...」'並且最好使它成爲'r'原始字符串。 –