Python 比较字符串忽略特殊字符 [英] Python compare strings ignore special characters
本文介绍了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
这篇关于Python 比较字符串忽略特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文