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"