一個出錯的例子
#coding:utf-8s = u'中文'f = open("test.txt","w")f.write(s)f.close()
原因是編碼方式錯誤,應該改為utf-8編碼
解決方案一:
#coding:utf-8s = u'中文'f = open("test.txt","w")f.write(s.encode("utf-8"))f.close()
解決方案二:
#coding:utf-8import sysreload(sys)sys.setdefaultencoding('utf-8') s = u'中文'f = open("test.txt","w")f.write(s)f.close()
以上這篇解決python中 f.write寫入中文出錯的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答