Python在寫入文件時中文報錯IOError: [Errno 22] invalid mode ('w') or filename

Python版本爲2.7.0,IDE爲PyCharm

f = open(filename, 'w')
f.write(html)


filename中包含中文,html爲一個網頁數據,結果報瞭如下的錯

  File "D:/PythonWorkSpace/test.py", line 42, in <module>
    tiebaSpider(fullUrl,beginPage,endPage)
  File "D:/PythonWorkSpace/test.py", line 31, in tiebaSpider
    writeFile(html,filename)
  File "D:/PythonWorkSpace/test.py", line 20, in writeFile
    f = open(filename, 'w')
IOError: [Errno 22] invalid mode ('w') or filename: '\xe7\xac\xac1\xe9\xa1\xb5.html'


將代碼修改爲:

    f = open(filename.decode('utf-8'), 'w')
    f.write(html)


成功運行,因爲是Python中的字符串的大概分爲爲str和Unicode兩種形式,其中str常用的編碼類型爲utf-8,gb2312,gbk等等,Python使用Unicode作爲編碼的基礎類型,open(filename, ‘w’)這個方法中,filename這個參數必須是Unicode編碼的參數。
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章