Encoding Chinese characters to open JSON file in python
Question:
I am unable to open a json file containing chinese and english characters. I have tried using-
with open('.//train//chess.json') as f:
d = json.load(f, encoding = "UTF-8")
I have also tried other encodings (Latin-1, UTF-16, UTF-32, BIG5, GB) too but none of them work. Here is the json file as it shows in notepad++ –
{
"name": "tournament_test_2",
"start_date": "2020-01-20",
"end_date": "2020-02-09",
"games": {
"tour_1": [
{
"white": "吕亚光",
"black": "胡建静",
"date": "2020-01-20",
"id": "tournament_test_2_1"
},
{
"white": "崔军凯",
"black": "陈森",
"date": "2020-01-20",
"id": "tournament_test_2_2"
},
{
"white": "丁晨彤",
"black": "董海非",
"date": "2020-01-20",
"id": "tournament_test_2_3"
},
{
"white": "傅星菲",
"black": "张春",
"date": "2020-01-20",
"id": "tournament_test_2_4"
},
{
"white": "魏辟涛",
"black": "黄凯文",
"date": "2020-01-20",
"id": "tournament_test_2_5"
}
]
},
"tours": 1,
"time_control": "classic"
}
Here is the image of the data for a clearer picture –
I get the following error –
File "C:UsersAsus-ROGDesktopMachine learninghands-onChesschess1.py", line 14, in
d = json.load(f, encoding = "UTF-8")
File "C:UsersAsus-ROGanaconda3envstflibjson_init_.py", line 293, in load
return loads(fp.read(),
File "C:UsersAsus-ROGanaconda3envstflibencodingscp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x90 in position 180: character maps to
Answers:
Your character decoding goes in the file open()
method, rather than the json load()
method:
import json
with open('chess.json', encoding='utf-8') as f:
d = json.load(f)
I am unable to open a json file containing chinese and english characters. I have tried using-
with open('.//train//chess.json') as f:
d = json.load(f, encoding = "UTF-8")
I have also tried other encodings (Latin-1, UTF-16, UTF-32, BIG5, GB) too but none of them work. Here is the json file as it shows in notepad++ –
{
"name": "tournament_test_2",
"start_date": "2020-01-20",
"end_date": "2020-02-09",
"games": {
"tour_1": [
{
"white": "吕亚光",
"black": "胡建静",
"date": "2020-01-20",
"id": "tournament_test_2_1"
},
{
"white": "崔军凯",
"black": "陈森",
"date": "2020-01-20",
"id": "tournament_test_2_2"
},
{
"white": "丁晨彤",
"black": "董海非",
"date": "2020-01-20",
"id": "tournament_test_2_3"
},
{
"white": "傅星菲",
"black": "张春",
"date": "2020-01-20",
"id": "tournament_test_2_4"
},
{
"white": "魏辟涛",
"black": "黄凯文",
"date": "2020-01-20",
"id": "tournament_test_2_5"
}
]
},
"tours": 1,
"time_control": "classic"
}
Here is the image of the data for a clearer picture –
I get the following error –
File "C:UsersAsus-ROGDesktopMachine learninghands-onChesschess1.py", line 14, in
d = json.load(f, encoding = "UTF-8")
File "C:UsersAsus-ROGanaconda3envstflibjson_init_.py", line 293, in load
return loads(fp.read(),
File "C:UsersAsus-ROGanaconda3envstflibencodingscp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: ‘charmap’ codec can’t decode byte 0x90 in position 180: character maps to
Your character decoding goes in the file open()
method, rather than the json load()
method:
import json
with open('chess.json', encoding='utf-8') as f:
d = json.load(f)