須要讀取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"