2017-10-18 4 views
0

Scrapy를 사용하여 일부 JSON 데이터를 'wotd-page-one.json'이라는 파일로 스크랩했습니다. JSON 데이터에는 스페인어 단어가 포함되어 있으며 악센트 부호가있는 문자는 유니 코드로 변환되었습니다. 이 데이터를로드하고 동일한 디렉토리 내의 python 스크립트로 usbale을 만들고 싶습니다. 각 JSON 키와 값을 개별적으로 작업하기 위해이 데이터를 목록에로드하려고합니다. 그러나 유니 코드와 JSON을 사용하여 많은 경험을 한 경험이 없기 때문에 이런 일이 발생하는 데 어려움을 겪고 있습니다. 아무도 파이썬 목록을 통해 이러한 데이터에 액세스 할 수있는 방법을 찾도록 도와 줄 수 있습니까? 이상적으로, ID는 data [2] == "DEF"data [3] == "유니 코드 문자가 latin-1로 변환 된 문자열"과 data [4] == "SENTENCE"data [5] 에서 == JSON 파일로파이썬에서 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

"어떤 유니 코드 문자가 포함 된 문자열은 라틴어 - 1로 변환", 당신은 한 번의 작업으로로드 할 수 있습니다. 그것은 파이썬 구조로 변환 될 것입니다 ...이 경우, 사전의 목록. 예를 들면 :

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)