2016-06-27 5 views
1

JSON으로로드 할 파일이 여러 개 있습니다. 모두 같은 방식으로 형식이 지정되어 있지만 그 중 하나에 대해서는 예외를 발생시키지 않고로드 할 수 없습니다. 이 파일을 찾을 수있는 곳입니다 : Python에서 json 파일로드

File

는 다음 코드를했다 :

def from_seed_data_extract_summoners(): 
    summonerIds = set() 
    for i in range(1,11): 
     file_name = 'data/matches%s.json' % i 
     print file_name 
     with open(file_name) as data_file:  
      data = json.load(data_file) 
     for match in data['matches']: 
      for summoner in match['participantIdentities']: 
       summonerIds.add(summoner['player']['summonerId']) 
    return summonerIds 

내가 다음 작업을 수행 할 때 오류가 발생합니다 : json.load(data_file). 나는 특수 문자가 있다고 생각하지만 그것을 찾을 수없고 대체 방법을 모른다. 생성 된 오류는 다음과 같습니다.

UnicodeDecodeError: 'utf8' codec can't decode byte 0xeb in position 6: invalid continuation byte 

내가 어떻게 탈 수 있는지 알고 있습니까? 데이터 = JSON : data_file에 오픈 (FILE_NAME)와

- FILE_NAME의 = 가진

답변

1

시도 :

json.loads(unicode(data_file.read(), errors='ignore')) 

나 :

json.loads(unidecode.unidecode(unicode(data_file.read(), errors='ignore'))) 

내가 변화했다

2
  1. 대체 file_name = 'data/matches%s.json' % i% I
  2. 오른쪽 구문 data = json.load(file_name)되지 않고, '데이터/% i.json 일치 "를 .load (data_file에)

EDIT :

def from_seed_data_extract_summoners(): 
summonerIds = set() 
    for i in range(1,11): 
     file_name = 'data/matches%i.json' % i 
     with open(file_path) as f: 
      data = json.load(f, encoding='utf-8') 
     for match in data['matches']: 
      for summoner in match['participantIdentities']: 
       summonerIds.add(summoner['player']['summonerId'])  
    return summonerIds 
,
+0

을 (두 번째를 들어, unidecode을 설치해야합니다) 다음 오류가 발생합니다. AttributeError : 'str'객체에 'read'속성이 없습니다. – mel

+1

답을 다시 작성하겠습니다. –

+1

https://docs.python.org/2/library/json.html#json.load 및 https를 확인하십시오. : //docs.python.org/2/library/json.html#json.load @mel –

2

JSON이 단순한 문자열이 아닌 유니 코드로 데이터를 강제 전송하려고합니다. 당신은 유니 코드로 강제 될 수없는 일부 내장 문자 (아마도 눈에 띄지 않는 공간 또는 뭔가)를 가지고 있습니다. JSON 파이썬에서 더 관리 개체를 만드는 방법에 대한 좋은 스레드가

How to get string objects instead of Unicode ones from JSON in Python?

.

1

시도 :

json.loads(data_file.read(), encoding='utf-8') 
+0

'ascii'코덱은 16260798 위치의 0xc3 바이트를 디코딩 할 수 없습니다. 서수는 범위 내에 없습니다 (128) – mel