Chinese encoding of Python files,

Source: Internet
Author: User

Chinese encoding of Python files,

 

Read/write Chinese

To read a UTF-8 encoded Chinese file, use the sublime text software to change it to a non-DOM encoding, and then use the following code:

With codecs. open (note_path, 'r + ', 'utf-8') as f:

Line = f. readline ()

Print line

 

In this way, the Chinese characters in the file can be correctly read.

 

Similarly, if you want to write Chinese characters to the created file, it is better to be similar to the above:

With codecs. open (st, 'a + ', 'utf-8') as book_note:

Book_note.write (st)

Create a Chinese File

Create a file with the read characters as the file name.

If you create a file using the string read above, the following error occurs:

St = digest_path + "\" + onenote [0] + ". txt"

Print st

With open (st, 'a + ') as book_note:

 

 

After debugging, it should be the last line break problem. When a name is generated, You can trip the character to get the file:

St = digest_path + "\" + onenote [0]. strip () + ". txt"

 

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.