【发布时间】:2019-09-19 07:12:04
【问题描述】:
我的字符串比较不起作用,
有什么想法吗?
a = person.category[0].lower()
b = to_delete[5].lower()
print("test ", repr(a), "type: ", type(a))
print("test ", repr(b), "type: ", type(b))
print(a == b)
print(a is b)
print("éclairage public" == b)
print("éclairage public" == a )
返回:
test 'éclairage public' type: <class 'str'>
test 'éclairage public' type: <class 'str'>
False
False
False
True
所以“b”没有预期的成分,但我不知道为什么!
【问题讨论】:
-
简单的
print(a)和print(b)的输出是什么? -
显然我希望一切都是真的!
-
如果您不提供
person.category和to_delete的内容,我们将无法重现该问题。 -
您可能遇到了 Unicode 规范化问题。比较
a.encode()和b.encode()的值 -
比较
b'e\xcc\x81'.decode()和b'\xc3\xa9'.decode()的结果。两者看起来都像é,但它们是两个不同的Unicode字符串。
标签: python python-3.x string compare string-comparison