[python语法语义] unicode写入文件出错.

ipython 2009-02-07
s='abc中国' ss=unicode(s,'gb2312') w=open('gg.txt','wb') w.write(ss) #出错了. Traceback (most recent call last): File "", line 1, in w.write(ss) UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) 应该如何解决?
姜太公 2009-02-08
具体为什么不太清楚,Python好像不能直接写入Unicode字符串,如果要写入,先编码
如f.write(s.encode('gbk'))
bohemia 2009-02-10
w=codecs.open('gg.txt','wb','utf16')

you need a encode before write; default system encoding will be used if you haven't set it;
Global site tag (gtag.js) - Google Analytics