开发者

UnicodeEncodeError: ascii when wishing to use unicode

I'm trying something like this:

outFile = open("file.txt", "wt",encoding='utf-8')
outFile.write(str(sentence))
outFile.close()

and getting the error:

UnicodeEncodeError: 'ascii' codec can't encode character '/x4e'.

why is an ascii encoder being used?

Am I right in saying that my string (str(sentence)) is in unicode? Then why is it not simply encoded as utf-8 when writen to file? This code gives no exception when run on ubuntu and windows, with the exception occuring on mac os x.

Seems to me that ascii is being used by default somewhere on my mac even though i explicitly state the use of utf-8

Please help,

Barr开发者_JAVA百科y


str() returns a string yes. And a str will be encoded when written, yes. I'm not entirely sure why the ascii encoding is being used (it is the default encoding in Python 2, but not in Python 3), but I'm even less sure why you do str(sentence). If you want to decode bytes you don' use str() you use .decode(). So start with removing the str() call.

You don't give a full traceback, but I'm guessing that it's the str(sentence) that gives the error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜