Python 比较字符串忽略特殊字符
问题描述:
我想比较两个字符串,以便比较时忽略特殊字符的差异.也就是说,
I want to compare two strings such that the comparison should ignore differences in the special characters. That is,
嘿,这是一个测试
应该与
喂!这是一个测试或"Hai 这是一个测试
Hai ! this is a test "or" Hai this is a test
有没有办法在不修改原始字符串的情况下做到这一点?
Is there any way to do this without modifying the original strings?
答
这会在进行比较之前删除标点符号和空格:
This removes punctuation and whitespace before doing the comparison:
In [32]: import string
In [33]: def compare(s1, s2):
...: remove = string.punctuation + string.whitespace
...: return s1.translate(None, remove) == s2.translate(None, remove)
In [34]: compare('Hai, this is a test', 'Hai ! this is a test')
Out[34]: True