Processing of UnicodeDecodeError when reading python files

Source: Internet
Author: User

After opening a file through open in python, The UnicodeDecodeError exception is thrown when the file contains the Chinese fd. readline. It can be basically determined that the encoding of the read file is a problem. Because the Read File uses the C ++ code written by VS, and the default storage encoding of vs is gb2312. It works when converting file encoding to UTF-8. The fundamental solution is to unify the code written by VS into the code of the UTF-8, but this part of the code is not maintained by me, and the code is still being added, other developers through VS quickly establish C ++ class, asking them to use the advanced save option to save as UTF-8 seems to have the difficulty of writing, can only temporarily solve this problem through themselves. Prototype of the open function: Specify the encoding of the open file by specifying the encoding parameter. The default value of encoding is the UTF-8, which can be obtained through fd. encoding. To automatically obtain the file encoding format, the chardet module must support it. The solution is to use UTF-8 to read files by default. When a UnicodeDecodeError error occurs, with open (fn, "r") as fd blocks will automatically disable fd, you can simply re-open the file using gbk encoding in bytes T.

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: 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.