I tried using some code like this to read a JSON file (encoded using UTF-8):
input = open("json/world_bank.json")
i=0
for l in input:
i+=1
print(i)
But I got a UnicodeDecodeError
. However, it started working once I tried explicitly specifying an encoding:
input = open("json/world_bank.json",encoding="utf8")
I thought the open
function would use "utf8"
as the default encoding? Why does it need to be specified?
sys.getfilesystemencoding()
return on your system?open("json/world_bank.json").encoding
as well?