Baumann
Baumann

Reputation: 33

Problems with Chinese in python

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

Answers (1)

juanpa.arrivillaga
juanpa.arrivillaga

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

Related Questions