Python read Chinese character encoding problem

Source: Internet
Author: User

Preface: A long time did not encounter coding problems, one did not pay attention to be tripped foot.

First, the editor is in ASICC encoding format, if you want to have Chinese in the program, you need to change to utf-8 format, add the following code can be broken:

<span style= "FONT-SIZE:18PX;" >import sys  Reload (SYS)  sys.setdefaultencoding (' UTF8 ')  </span>
or refer to the Halogen master reprint of this http://blog.csdn.net/u010454729/article/details/40476015, another way can be broken.

Second, when reading Chinese documents, the halogen master naïve to think that the character length is based on the length of the Chinese, but after reading the discovery is based on the length of the character, a Chinese character length of 3. Found codecs can be broken:

<span style= "FONT-SIZE:18PX;" >import codecswith codecs.open ("XXX.txt", encoding = "utf-8") as F:    text  =  f.readlines () for I in Text:
   i = I.strip ("\ n")    print I,len (i) </span>


Python read Chinese character encoding problem

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.