如何检查字符串是否由 Python 中给定字符以外的字符组成?
How do I check if a string is composed up of characters other than my given characters in Python?
我的数据框中有一个 Phone 数字列,其中包含很多噪音。所以我想检查是否有任何行由 +、反斜杠常量和 0-9 以外的任何字符组成。
如果需要,我只想为所有其他行提取那些行,我希望将这些字符替换为“”。我怎样才能做到这一点?由于我的数据集太大,我发布了我的问题的最小版本。
这是我试过的
ph = ['00 9108214702Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x83Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¢Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82Â\x80Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¬', '080 26600704\r\n+91 9342593424']
for x in ph:
print(re.match('^[\+0-9\r\n]+$', x))
这给了我 none
其中 +0-9\r\n 是允许的字符。所以我想要的只是那些不匹配+、\r、\n、0-9和space的字符。
对于这个问题,我已经尝试了所有可能的建议,其中 none 个对我有用。
regex solution:
import re
ph = ['00 9108214702Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x83Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¢Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82Â\x80Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¬', '080 26600704\r\n+91 9342593424']
numbers = [re.findall('[\+0-9\r\n\s]+', x)[0] for x in ph]
non regex solution:
ph = ['00 9108214702Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x83Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¢Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82Â\x80Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¬', '080 26600704\r\n+91 9342593424']
numbers = ["".join([c for c in x if c in "0123456789\n\r+"]) for x in ph]
非正则表达式解决方案,使用集合。您可以尝试哪种解决方案更快。我想对于长字符串来说正则表达式是最好的。
allowed=set('abcde')
if set(ph) - allowed:
print('String contains not allowed characters')
我的数据框中有一个 Phone 数字列,其中包含很多噪音。所以我想检查是否有任何行由 +、反斜杠常量和 0-9 以外的任何字符组成。
如果需要,我只想为所有其他行提取那些行,我希望将这些字符替换为“”。我怎样才能做到这一点?由于我的数据集太大,我发布了我的问题的最小版本。
这是我试过的
ph = ['00 9108214702Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x83Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¢Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82Â\x80Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¬', '080 26600704\r\n+91 9342593424']
for x in ph:
print(re.match('^[\+0-9\r\n]+$', x))
这给了我 none
其中 +0-9\r\n 是允许的字符。所以我想要的只是那些不匹配+、\r、\n、0-9和space的字符。
对于这个问题,我已经尝试了所有可能的建议,其中 none 个对我有用。
regex solution:
import re
ph = ['00 9108214702Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x83Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¢Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82Â\x80Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¬', '080 26600704\r\n+91 9342593424']
numbers = [re.findall('[\+0-9\r\n\s]+', x)[0] for x in ph]
non regex solution:
ph = ['00 9108214702Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x83Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¢Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82Â\x80Ã\x83Â\x83Ã\x82Â\x83Ã\x83Â\x82Ã\x82Â\x82Ã\x83Â\x83Ã\x82Â\x82Ã\x83Â\x82Ã\x82¬', '080 26600704\r\n+91 9342593424']
numbers = ["".join([c for c in x if c in "0123456789\n\r+"]) for x in ph]
非正则表达式解决方案,使用集合。您可以尝试哪种解决方案更快。我想对于长字符串来说正则表达式是最好的。
allowed=set('abcde')
if set(ph) - allowed:
print('String contains not allowed characters')