我正在使用python 2.6.5 我想將一些日文字符寫入文件。 我得到這個錯誤&我不知道如何改變編碼。如何將unicode字符串寫入文件?
Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01)
[GCC 4.3.4 20090804 (release) 1] on cygwin
>>> s = u'\u5E73\u621015'
>>> with open("yop", "wb") as f:
... f.write(s + "\n");
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1:
ordinal not in range(128)
>>> type(s)
<type 'unicode'>
謝謝您的建議。 – 2011-03-31 00:41:08