Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的中文问题_Python_Unicode - Fatal编程技术网

python中的中文问题

python中的中文问题,python,unicode,Python,Unicode,我有一个用python 2.7编写的中文程序,它给了我一些问题 代码: 我得到一个错误: test_chinese.py:6:UnicodeWarning:Unicode相等比较无法将两个参数转换为Unicode-将它们解释为不相等 如果(命令==u)����".编码('utf-8'): 有什么不正确的地方吗?您不需要对unicode文本进行编码你好“,所以只需使用: import sys command = raw_input(">>> ").decode(sys.stdin

我有一个用python 2.7编写的中文程序,它给了我一些问题

代码:

我得到一个错误:

test_chinese.py:6:UnicodeWarning:Unicode相等比较无法将两个参数转换为Unicode-将它们解释为不相等 如果(命令==u)����".编码('utf-8'):


有什么不正确的地方吗?

您不需要对unicode文本进行
编码你好“
,所以只需使用:

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")

如果(命令==u),请尝试此代码你好“”:而不是
if(命令==u)你好“.encode('utf-8'):
也许你应该给出一个有效的理由,说明你为什么鼓励OP在这个问题上使用python2case@repzero对不起,什么?我鼓励他们使用Python3,主要是因为它提供了更精简的unicode支持。好吧,请在您的回复中说明这一点!如果没有支持,不要提出建议!
command = input(">>> ")
if command == "你好":
    print("etc")