Using the Python module unicode-nazi to detect unicode issues, I am running into this warning:
/home/dotancohen/unicode-test.py:51: UnicodeWarning: Implicit conversion of unicode to str
print("Here is a phrase: " + str(phrase))
Since phrase is being explicitly cast to string, where is the implicit conversion? Surely "Here is a phrase: " is a string, as it is not preceded by u.
dda
6,2212 gold badges27 silver badges37 bronze badges
asked Jun 11, 2013 at 13:44
dotancohen
31.8k43 gold badges147 silver badges208 bronze badges
-
I don't know if that's what it complains about, but it sure wouldn't hurt to explicitly encode...user395760– user3957602013年06月11日 13:46:15 +00:00Commented Jun 11, 2013 at 13:46
-
phrase.encode('1252')pylover– pylover2013年06月11日 13:47:37 +00:00Commented Jun 11, 2013 at 13:47
1 Answer 1
You need to encode the phrase unicode value explicitly:
print("Here is a phrase: " + phrase.encode('some_codec'))
str() on a unicode value implicitly encodes that value, using the default codec (ASCII on Python 2).
answered Jun 11, 2013 at 13:46
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
dotancohen
I see! The warning message should probably say "Implicit encoding of unicode to some_codec in conversion to str". Thank you!
lang-py