"Unicode在哪里?"

2024-06-12 08:50:17 发布

您现在位置:Python中文网/ 问答频道 /正文

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

/home/dotancohen/unicode-test.py:51: UnicodeWarning: Implicit conversion of unicode to str
print("Here is a phrase: " + str(phrase))

既然phrase正被显式地转换为字符串,那么隐式转换在哪里?当然,"Here is a phrase: "是一个字符串,因为它前面没有u。在


Tags: 模块字符串pytest警告homehereis
1条回答
网友
1楼 · 发布于 2024-06-12 08:50:17

您需要显式地编码phraseunicode值:

print("Here is a phrase: " + phrase.encode('some_codec'))

unicode值上的str()使用默认的编解码器(python2上的ASCII)隐式地对该值进行编码。在

相关问题 更多 >