Json.dumps error: ' UTF8 ' codec can ' t decode byte solution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.