The perfect solution of Python's acquisition of Chinese garbled problem _python

Source: Internet
Author: User

In recent days, when the acquisition of a page most of the Web page OK, a small number of pages garbled problems, debugging a few days, and finally found that there are some illegal characters caused. Hereby record

1. Under normal circumstances. can use

Import Chardet

thischarset = Chardet.detect (STRs) ["Encoding"]

To get the encoding of the file or page

or directly grab the page charset = xxxx to get

2. The encoding that is specified when a special character is encountered in the content will cause garbled characters . That is, the illegal character in the content is caused, can use the code to ignore the illegal character of the way to deal with.

STRs = Strs.decode ("UTF-8", "ignore"). Encode ("UTF-8")

The second parameter of decode represents the way in which illegal characters are encountered

This parameter defaults to throwing an exception.

The above is a small series for you to bring Python collection of Chinese garbled problem of the perfect solution to all the content, I hope to help you, a lot of support cloud Habitat Community ~

Related Article

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.