Reputation: 33
I have a Chinese program in python 2.7 that is giving me some problems
Code:
# -*- coding: gb2312 -*-
import sys
command = raw_input(">>> ").decode(sys.stdin.encoding)
if (command == u"你好".encode('utf-8')):
print "etc"
I get the error:
test_chinese.py:6: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal if (command == u"����".encode('utf-8')):
Is there anything incorrect?
Upvotes: 1
Views: 120
Reputation: 95873
You don't need to encode
your unicode literal: u"你好"
, so just use:
import sys
command = raw_input(">>> ").decode(sys.stdin.encoding)
if command == u"你好":
print "etc"
Honestly, you should just use Python 3. Support for unicode is much better. Indeed, str
are now sequences of unicode points, unlike "byte strings" in Python 2, which have been changed to the bytes
data type. In Python 3, all you would need to do is:
command = input(">>> ")
if command == "你好":
print("etc")
Upvotes: 1