问题与中国在python

问题描述:

我在Python 2.7中国的计划,是给我一些问题问题与中国在python

代码:

# -*- coding: gb2312 -*- 
import sys 
command = raw_input(">>> ").decode(sys.stdin.encoding) 
if (command == u"你好".encode('utf-8')): 
    print "etc" 

我得到的错误:

test_chinese.py:6: UnicodeWarning:Unicode等于比较无法将两个参数转换为Unicode - 将它们解释为不相等 if(command == u“ ”.encode('utf-8')):

有什么不对吗?

+0

尝试下面的代码if(command == u“你好”):'而不是'if(command == u“你好”.encode ('utf-8')):' – Minji

你不需要encode您的Unicode文本:u"你好",所以只需使用:

import sys 
command = raw_input(">>> ").decode(sys.stdin.encoding) 
if command == u"你好": 
    print "etc" 

老实说,你应该只使用Python 3对Unicode的支持要好得多。的确,str现在是unicode点的序列,与Python 2中的“字节串”不同,后者已更改为bytes数据类型。在Python 3中,所有你需要做的是:

command = input(">>> ") 
if command == "你好": 
    print("etc") 
+0

也许你应该给出有效的一个原因,为什么你鼓励OP在这种情况下使用python2 – repzero

+1

@repzero抱歉,什么?我鼓励他们使用* Python 3 *,主要是为了更简化的unicode支持。 –

+0

把这个放在你的回应中!不要在没有备份的情况下提出建议! – repzero