When I try to load a json file using following code, it gives some u characters:
import json
with open('strings.json') as json_data:
d = json.load(json_data)
print(d)
[{u'goat': 45}, {u'chicken': 45}]
what is going on ? any ideas ?
When I try to load a json file using following code, it gives some u characters:
import json
with open('strings.json') as json_data:
d = json.load(json_data)
print(d)
[{u'goat': 45}, {u'chicken': 45}]
what is going on ? any ideas ?
The u
that you see stands for unicode
, which is a really common codification system that allows you to manage almost all characters present in all living languages.
It is a good idea to keep your string codified in unicode, but if you want to print out a string without the u
at the beginning, you can use:
print(mystring.encode("utf-8"))
Here you can follow a SO discussion on the u
prefix, where someone also cite this amazing article on codification.
You can not use ''
for JSON format. You have to use ""
for JSON format. And You had used u'something'
Unicode format. No need to use Unicode in this section. Sometime need to encode b''
for Binary.
So your code look like
import json
with open('strings.json', encoding("utf-8")) as json_data:
d = json.load(json_data)
print(d)
#[{"goat": 45},{"chicken": 45}]