【发布时间】:2011-08-16 03:45:50
【问题描述】:
我需要从字符串中删除所有特殊字符、标点符号和空格,以便我只有字母和数字。
【问题讨论】:
我需要从字符串中删除所有特殊字符、标点符号和空格,以便我只有字母和数字。
【问题讨论】:
10 年后,我在下面写了最好的解决方案。 您可以从字符串中删除/清除所有特殊字符、标点符号、ASCII 字符和空格。
from clean_text import clean
string = 'Special $#! characters spaces 888323'
new = clean(string,lower=False,no_currency_symbols=True, no_punct = True,replace_with_currency_symbol='')
print(new)
Output ==> 'Special characters spaces 888323'
you can replace space if you want.
update = new.replace(' ','')
print(update)
Output ==> 'Specialcharactersspaces888323'
【讨论】:
我为提供的答案计时。
import re
re.sub('\W+','', string)
通常比第二快提供的最佳答案快 3 倍。
使用此选项时应小心。使用此方法可能无法对某些特殊字符(例如 ø)进行条带化。
看到这一点后,我有兴趣通过找出哪些执行时间最短来扩展提供的答案,因此我通过timeit 对照两个示例字符串检查了一些建议的答案:
string1 = 'Special $#! characters spaces 888323'string2 = 'how much for the maple syrup? $20.99? That s ridiculous!!!''.join(e for e in string if e.isalnum())
string1 - 结果:10.7061979771string2 - 结果:7.78372597694import re
re.sub('[^A-Za-z0-9]+', '', string)
string1 - 结果:7.10785102844string2 - 结果:4.12814903259import re
re.sub('\W+','', string)
string1 - 结果:3.11899876595string2 - 结果:2.78014397621以上结果是平均返回结果最低的乘积:repeat(3, 2000000)
示例 3 可以比 示例 1 快 3 倍。
【讨论】:
''.join([*filter(str.isalnum, string)])
这将从字符串中删除所有特殊字符、标点符号和空格,并且只包含数字和字母。
import re
sample_str = "Hel&&lo %% Wo$#rl@d"
# using isalnum()
print("".join(k for k in sample_str if k.isalnum()))
# using regex
op2 = re.sub("[^A-Za-z]", "", sample_str)
print(f"op2 = ", op2)
special_char_list = ["$", "@", "#", "&", "%"]
# using list comprehension
op1 = "".join([k for k in sample_str if k not in special_char_list])
print(f"op1 = ", op1)
# using lambda function
op3 = "".join(filter(lambda x: x not in special_char_list, sample_str))
print(f"op3 = ", op3)
【讨论】:
这将删除除空格以外的所有非字母数字字符。
string = "Special $#! characters spaces 888323"
''.join(e for e in string if (e.isalnum() or e.isspace()))
特殊字符空格 888323
【讨论】:
对于其他语言,如德语、西班牙语、丹麦语、法语等包含特殊字符(如德语“Umlaute”为ü、ä、ö),只需将这些添加到正则表达式搜索字符串:
德语示例:
re.sub('[^A-ZÜÖÄa-z0-9]+', '', mystring)
【讨论】:
string.punctuation 包含以下字符:
'!"#$%&\'()*+,-./:;?@[\]^_`{|}~'
您可以使用 translate 和 maketrans 函数将标点符号映射到空值(替换)
import string
'This, is. A test!'.translate(str.maketrans('', '', string.punctuation))
输出:
'This is A test'
【讨论】:
这是一个匹配非字母或数字的字符串的正则表达式:
[^A-Za-z0-9]+
这是执行正则表达式替换的 Python 命令:
re.sub('[^A-Za-z0-9]+', '', mystring)
【讨论】:
+ 量词来稍微提高它的效率。)
[^A-Za-z0-9 ]+
这可以在没有正则表达式的情况下完成:
>>> string = "Special $#! characters spaces 888323"
>>> ''.join(e for e in string if e.isalnum())
'Specialcharactersspaces888323'
你可以使用str.isalnum:
S.isalnum() -> bool Return True if all characters in S are alphanumeric and there is at least one character in S, False otherwise.
如果您坚持使用正则表达式,其他解决方案也可以。但是请注意,如果它可以在不使用正则表达式的情况下完成,那是最好的方法。
【讨论】:
isalnum() 和正则表达式版本都进行了基准测试,而正则表达式版本的速度提高了 50-75%
我认为 filter(str.isalnum, string) 有效
In [20]: filter(str.isalnum, 'string with special chars like !,#$% etcs.')
Out[20]: 'stringwithspecialcharslikeetcs'
在 Python3 中,filter( ) 函数将返回一个可迭代对象(而不是与上面不同的字符串)。必须重新加入才能从 itertable 中获取字符串:
''.join(filter(str.isalnum, string))
或者通过list加入使用(not sure but can be fast a bit)
''.join([*filter(str.isalnum, string)])
注意:在[*args] 中解包从Python >= 3.5 有效
【讨论】:
map、filter 和 reduce 返回可迭代对象。仍然在 Python3+ 中,我更喜欢''.join(filter(str.isalnum, string))(或在加入时传递列表使用''.join([*filter(str.isalnum, string)]))而不是接受的答案。
''.join(filter(str.isalnum, string)) 是对filter(str.isalnum, string) 的改进,至少可以阅读。这真的是 Pythreenic(是的,你可以使用那个)的方式来做到这一点吗?
filter(str.isalnum, string) 不要在Python3 中返回字符串,因为Python-3 中的filter( ) 返回迭代器而不是参数类型,这与Python-2 不同。+
与使用正则表达式的其他人不同,我会尝试排除所有不是我想要的字符,而不是明确列举我不想要的。
例如,如果我只想要从“a 到 z”(大写和小写)和数字的字符,我会排除其他所有字符:
import re
s = re.sub(r"[^a-zA-Z0-9]","",s)
这意味着“用一个空字符串替换每个不是数字的字符,或者'a to z'或'A to Z'范围内的字符”。
事实上,如果你在你的正则表达式的第一个位置插入特殊字符^,你会得到否定。
额外提示:如果您还需要小写结果,则可以使正则表达式更快更容易,只要您现在找不到任何大写即可。
import re
s = re.sub(r"[^a-z0-9]","",s.lower())
【讨论】:
import re
my_string = """Strings are amongst the most popular data types in Python. We can create the strings by enclosing characters in quotes. Python treats single quotes the
同双引号。"""
# if we need to count the word python that ends with or without ',' or '.' at end
count = 0
for i in text:
if i.endswith("."):
text[count] = re.sub("^([a-z]+)(.)?$", r"\1", i)
count += 1
print("The count of Python : ", text.count("python"))
【讨论】:
s = re.sub(r"[-()\"#/@;:<>{}`+=~|.!?,]", "", s)
【讨论】:
使用翻译:
import string
def clean(instr):
return instr.translate(None, string.punctuation + ' ')
警告:仅适用于 ascii 字符串。
【讨论】:
TypeError: translate() takes exactly one argument (2 given)
translate 与 Python3 一起使用。
import re
abc = "askhnl#$%askdjalsdk"
ddd = abc.replace("#$%","")
print (ddd)
你会看到你的结果
'askhnlaskdjalsdk
【讨论】:
re 但从未使用过它。您的 replace 标准仅适用于该特定字符串。如果你的字符串是abc = "askhnl#$%!askdjalsdk" 怎么办?我认为除了#$% 模式之外不会有任何作用。可能想要调整它
更短的方式:
import re
cleanString = re.sub('\W+','', string )
如果您希望单词和数字之间有空格,请将 '' 替换为 ' '
【讨论】:
r'\W+' - 稍微偏离主题(而且非常迂腐),但我建议所有正则表达式模式都是raw strings
_ 的简单更改:r"[^A-Za-z]+" 而不是r"\W+"
#!/usr/bin/python
import re
strs = "how much for the maple syrup? $20.99? That's ricidulous!!!"
print strs
nstr = re.sub(r'[?|$|.|!]',r'',strs)
print nstr
nestr = re.sub(r'[^a-zA-Z0-9 ]',r'',nstr)
print nestr
您可以添加更多特殊字符,将被替换为 '' 没有任何意义,即它们将被删除。
【讨论】:
最通用的方法是使用对每个字符进行分类的 unicodedata 表的“类别”。例如。以下代码仅根据类别过滤可打印字符:
import unicodedata
# strip of crap characters (based on the Unicode database
# categorization:
# http://www.sql-und-xml.de/unicode-database/#kategorien
PRINTABLE = set(('Lu', 'Ll', 'Nd', 'Zs'))
def filter_non_printable(s):
result = []
ws_last = False
for c in s:
c = unicodedata.category(c) in PRINTABLE and c or u'#'
result.append(c)
return u''.join(result).replace(u'#', u' ')
查看上面给出的所有相关类别的 URL。你当然也可以过滤 按标点符号分类。
【讨论】:
$ 是什么意思?
假设您想要使用正则表达式并且您想要/需要 2to3-ready 的 Unicode 认知 2.x 代码:
>>> import re
>>> rx = re.compile(u'[\W_]+', re.UNICODE)
>>> data = u''.join(unichr(i) for i in range(256))
>>> rx.sub(u'', data)
u'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\xaa\xb2 [snip] \xfe\xff'
>>>
【讨论】: