unicode在哪里?

dot*_*hen 3 python unicode

使用Python模块unicode-nazi来检测unicode问题,我遇到了这个警告:

/home/dotancohen/unicode-test.py:51:UnicodeWarning:将unicode隐式转换为str
print("这是一个短语:"+ str(短语))

由于phrase式转换为字符串,隐式转换在哪里?肯定"Here is a phrase: "是一个字符串,因为它没有前面u.

Mar*_*ers 6

您需要phrase显式编码unicode值:

print("Here is a phrase: " + phrase.encode('some_codec'))
Run Code Online (Sandbox Code Playgroud)

str() 在unicode值上使用默认编解码器(Python 2上的ASCII)隐式编码该值.