2017-10-18 83 views
0

我使用Scrapy將一些JSON數據報廢爲名爲「wotd-page-one.json」的文件。 JSON數據包含一些西班牙語單詞,重音字母被轉換爲Unicode。我想加載這些數據並在同一個目錄下使用python腳本進行製作。我正嘗試將這些數據加載到列表中以分別處理每個JSON密鑰和值。但是,由於我沒有使用Unicode和JSON的豐富經驗,所以我很難做到這一點。任何人都可以請幫助我找到一種方法,使這些數據可以通過Python列表訪問。理想情況下,我想使它像數據[2] ==「DEF」數據[3] ==「字符串與任何Unicode字符轉換爲拉丁-1」和數據[4] ==「SENTENCE」數據[5] ==「字符串與任何Unicode字符轉換爲Latin-1的」在Python中使用包含Unicode的報廢的JSON數據

Python file: 

    data=[] 
    with open('wotd-page-one.json', encoding='utf-8') as f: 
    for line in f: 
     line = line.replace('\n', '') 
     data.append(line) 
    print(data) 


    JSON file: 
[ 
{"TRANSLATION": "I don't like how that guy's whistling; it gives me the creeps.", "WORD": "silbar", "DEF": "to whistle", "SENTENCE": "No me gusta c\u00f3mo silba ese se\u00f1or; me da escalofr\u00edos."}, 
{"TRANSLATION": "\"Is somebody there?\" asked the boy in a startled voice.", "WORD": "sobresaltado", "DEF": "startled", "SENTENCE": "\"\u00bfHay alguien aqu\u00ed?\" pregunt\u00f3 el ni\u00f1o con voz sobresaltada."}, 
{"TRANSLATION": "Carla made a face at me when I asked her if she was scared.", "WORD": "la mueca", "DEF": "face", "SENTENCE": "Carla me hizo una mueca cuando le pregunt\u00e9 si ten\u00eda miedo."}, 
{"TRANSLATION": "The teacher tapped the board with the chalk.", "WORD": "golpetear", "DEF": "to tap", "SENTENCE": "El maestro golpete\u00f3 el pizarr\u00f3n con la tiza."} 
    ] 

Output: 
['[', 
'{"TRANSLATION": "I don\'t like how that guy\'s whistling; it gives me the creeps.", "WORD": "silbar", "DEF": "to whistle", "SENTENCE": "No me gusta c\\u00f3mo silba ese se\\u00f1or; me da escalofr\\u00edos."},', ' 
{"TRANSLATION": "\\"Is somebody there?\\" asked the boy in a startled voice.", "WORD": "sobresaltado", "DEF": "startled", "SENTENCE": "\\"\\u00bfHay alguien aqu\\u00ed?\\" pregunt\\u00f3 el ni\\u00f1o con voz sobresaltada."},', ' 
{"TRANSLATION": "Carla made a face at me when I asked her if she was scared.", "WORD": "la mueca", "DEF": "face", "SENTENCE": "Carla me hizo una mueca cuando le pregunt\\u00e9 si ten\\u00eda miedo."},', ' 
{"TRANSLATION": "The teacher tapped the board with the chalk.", "WORD": "golpetear", "DEF": "to tap", "SENTENCE": "El maestro golpete\\u00f3 el pizarr\\u00f3n con la tiza."}', ']'] 

回答

1

隨着JSON文件,可以在一個操作中加載它。它將變成一個Python結構......在這種情況下,是一個字典列表。例如:

import json 

with open('wotd-page-one.json') as f: 
    data = json.load(f) 

for d in data: 
    print(d['SENTENCE']) 

輸出:

No me gusta cómo silba ese señor; me da escalofríos. 
"¿Hay alguien aquí?" preguntó el niño con voz sobresaltada. 
Carla me hizo una mueca cuando le pregunté si tenía miedo. 
El maestro golpeteó el pizarrón con la tiza. 
0

JSON文件的第一行被讀"[",那麼它是你試圖解析它然而拋出一個異常,因爲這不是有效的json格式。通過一行一行閱讀,你忽略了文件的其餘部分,所以你不應該這樣做。相反,只需使用json.load就可以:

with open("wotd-page-one.json") as f: 
    data = json.load(f)