2017-07-14 128 views
0

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

你知道語法有什麼問題嗎?

原始代碼再次由數據發送者給出,所以我確信代碼是正確的。當我將「路徑」更改爲我的文件目錄時,我認爲我做錯了什麼。

謝謝。

+0

'高清解析(C:\用戶\菊花\研究\學習\亞馬遜\ reviews_Video_Games.json.gz):'? –

+0

該功能是爲了按原樣使用。 'path'是您在調用函數時提供的參數,而不是您在函數定義本身中應該更改的內容。 – jasonharper

+0

這不是它的工作原理。當你調用函數時,你必須替換這個值,例如''in l in parse(r「C:\ Users \ daisy \ Research \ study \ Amazon \ reviews_Video_Games.json.gz」):'另外,別忘了'「...」'並且最好使它成爲'r'原始字符串。 –

回答

0

你不能定義這樣的函數。

def parse(file_path): 
    g = gzip.open(file_path, 'r') 
    for l in g: 
    yield json.dumps(eval(l)) 

parse(r"C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz") 

雖然你可以設置像這樣的默認值:

def parse(file_path=r"C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz"): 
    g = gzip.open(file_path, 'r') 
    for l in g: 
    yield json.dumps(eval(l)) 

parse() 

更新編碼問題

>>> "C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz" 
    File "<stdin>", line 1 
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape 
>>> "C:\\Users\\daisy\\Research\\study\\Amazon\\reviews_Video_Games.json.gz" 
'C:\\Users\\daisy\\Research\\study\\Amazon\\reviews_Video_Games.json.gz' 
>>> r"C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz" 
'C:\\Users\\daisy\\Research\\study\\Amazon\\reviews_Video_Games.json.gz' 
+0

謝謝,Ipiner。我運行了第二個代碼,但是得到了與下面相同的錯誤,即'SyntaxError:(unicode error)'unicodeescape'編解碼器無法解碼位置2-3中的字節:截斷\ UXXXXXXXX轉義'。你有什麼想法如何解決這個問題? – Emily

+0

將來請留下原始問題並在底部添加新內容以免超出任何答案。您的新錯誤是一個編碼問題,請跳過您的反斜槓或將字符串標記爲原始的r'r「C:\ Users \ daisy \ Research \ study \ Amazon \ reviews_Video_Games.json.gz」 – lpiner