问题与中国在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')):
有什么不对吗?
答
你不需要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")
尝试下面的代码if(command == u“你好”):'而不是'if(command == u“你好”.encode ('utf-8')):' – Minji