须要读取utf-8编码的中文文件,先利用sublime text软件将它改为无DOM的编码,而且在第一行写:python
# encoding: utf-8
而后用如下代码:编码
with codecs.open(note_path, 'r+','utf-8') as f: line=f.readline() print line
这样就能够正确地读出文件里面的中文字符了。spa
固然,前面的方法只能读一行,要读出全部数据,就使用readlines(),而后再遍历:调试
with codecs.open(note_path, 'r+','utf-8') as f: lines=f.readlines() for problemName in lines:
一样的,若是要在建立的文件中写入中文,最好也和上面差很少:code
with codecs.open(st,'a+','utf-8') as book_note: book_note.write(st)
而后以读出的字符为文件名,建立文件。blog
若是直接用上面读出来的字符串建立文件,则会出现:ip
st=digest_path+"\\"+onenote[0]+".txt" print st with open(st,'a+') as book_note:
通过调试,应该是最后一个换行符的问题,在生成名字的时候,将字符trip一下,就可以获得文件:utf-8
st=digest_path+"\\"+onenote[0].strip()+".txt"