One time during the use of Json.dumps (), an error message appears:
ERROR: "Unicodedecodeerror: ' UTF8 ' codec can ' t decode byte 0xe1 in position 5:unexpected end of data"
We know by error that it is definitely a coding problem and finding the problem is not difficult to solve ...
Normal Condition:
1234567 |
#引入json模块 import json # Define a dictionary dit = { ' a ' : 111 ' B ' : 222 ' C ' : 333 } #把字典转换成json字符串 #运行结果为 ' { "A": 111, "B": 222, "C": 333} ' json.dumps (DIT) |
This is normal, but if a special character is present in the dictionary, the error will be reported.
How to solve?
Solution:
We just need to Unicode the string, ignore the error, the code is as follows:
1 |
unicode ( value, errors = ‘ignore‘ ) |
Note, however, that value here must be a string, where value is 111, 222, 333 above
Then the string Unicode, and then use the top code for JSON encode is OK, the problem is solved ...
Json.dumps error: ' UTF8 ' codec can ' t decode byte solution