将用户输入的字符串与另一个字符串进行比较的最佳方法是什么?
例如:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
user_input = raw_input("Please, write árido: ").decode("utf8")
if u"árido" == user_input:
print "OK"
else:
print "FALSE"
编辑:
这个
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from unicodedata import normalize
import sys
uinput2 = "árbol"
uinput = raw_input("type árbol: ")
print "Encoding %s" % sys.stdout.encoding
print "User Input \t\tProgram Input"
print "-"*50
print "%s \t\t\t%s \t(raw value)" % (uinput, uinput2)
print "%s \t\t\t%s \t(unicode(value))" % (unicode(uinput), unicode(uinput2))
print "%s \t\t\t%s \t(value.decode('utf8'))" % (uinput.decode("utf-8"), uinput2.decode("utf-8"))
print "%s \t\t\t%s \t(normalize('NFC',value))" % (normalize("NFC",uinput.decode("utf-8")), normalize("NFC",uinput2.decode("utf-8")));
print "\n\nUser Input \t\tProgram Input (Repr)"
print "-"*50
print "%s \t%s" % (repr(uinput),repr(uinput2))
print "%s \t%s \t(unicode(value))" % (repr(unicode(uinput)), repr(uinput2))
print "%s \t%s \t(value.decode('utf8'))" % (repr(uinput.decode("utf-8")), repr(uinput2.decode("utf-8")))
print "%s \t%s \t(normalize('NFC',value)))" % (repr(normalize("NFC",uinput.decode("utf-8"))), repr(normalize("NFC",uinput2.decode("utf-8"))));
印刷:
type árbol: árbol
Encoding utf-8
User Input Program Input
--------------------------------------------------
árbol árbol (raw value)
árbol árbol (unicode(value))
árbol árbol (value.decode('utf8'))
árbol árbol (normalize('NFC',value))
User Input Program Input (Repr)
--------------------------------------------------
'\xc3\x83\xc2\xa1rbol' u'\xe1rbol'
u'\xc3\xa1rbol' u'\xe1rbol' (unicode(value))
u'\xc3\xa1rbol' u'\xe1rbol' (value.decode('utf8'))
u'\xc3\xa1rbol' u'\xe1rbol' (normalize('NFC',value)))
任何想法?当我使用 Java 等其他语言时,我没有问题。这只发生在我身上的python。我正在使用 Eclipse。
提前致谢 :)