mel mel - 5 months ago 43
JSON Question

Loading a json file in python

I've got multiple file to load as JSON, they are all formatted the same way but for one of them I can't load it without raising an exception. This is where you can find the file:

File

I did the following code:

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


The error occurs when I do the following:
json.load(data_file)
. I suppose there is a special character but I can't find it and don't know how to replace it. The error generated is:

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


Do you know how I can get ride of it?

ntg ntg
Answer

Try:

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

or :

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

(for the second, you would need to install unidecode)