Total Pageviews

2018/03/09

[Python] UnicodeDecodeError: 'cp950' codec can't decode byte 0x99 in position 241: illegal multibyte sequence

Problem
When I try to read a file via Python, I get an error message as following:
1
2
3
4
Traceback (most recent call last):
  File "D:\work\myworkspace\PyTest\src\test\getdata.py", line 11, in <module>
    json = json_file.read()
UnicodeDecodeError: 'cp950' codec can't decode byte 0x99 in position 241: illegal multibyte sequence

Here has the code snippet:
json_file = open("D:/test/test.json", "r")
json = json_file.read()
print(json)        
json_file.close()


How-To
Remember add encoding when opening file, here has the updated code snippet:
json_file = open("D:/test/test.json", "r", encoding='utf8')
json = json_file.read()
print(json)        
json_file.close()


No comments: