Python Unicode Equal Comparison Failed In Terminal But Working Under Spyder Editor
Solution 1:
You are comparing a byte string (type str
) with a unicode
value. Spyder has changed the default encoding from ASCII to UTF-8, and Python does an implicit conversion between byte strings and unicode
values when comparing the two types. Your byte strings are encoded to UTF-8, so under Spyder that comparison succeeds.
The solution is to not use byte strings, use unicode
literals for your two test values instead:
test1 = u"Tarn"
test2 = u"Rhône-Alpes"
Changing the system default encoding is, in my opinion, a terrible idea. Your code should use Unicode correctly instead of relying on implicit conversions, but to change the rules of implicit conversions only increases the confusion, not make the task any easier.
Solution 2:
Just using depname = rec['DEP']
should work as you have already declared the encoding.
If you print some_french_deps[2]
it will print Rhône-Alpes
so your comparison will work.
Solution 3:
As you are comparing a string object with a unicode object, python throws this warning.
To fix this, you can write
test1 = "Tarn"test2 = "Rhône-Alpes"
as
test1 = u"Tarn"
test2 = u"Rhône-Alpes"
where the 'u' indicates it is a unicode object.
Post a Comment for "Python Unicode Equal Comparison Failed In Terminal But Working Under Spyder Editor"