2024-04-23 21:25:41 发布
网友
我要从文档中删除以下每个特殊字符:
symbols = {`,~,!,@,#,$,%,^,&,*,(,),_,-,+,=,{,[,],},|,\,:,;,",<,,,>,.,?,/}
我之所以不这么做是因为:
document = re.sub(r'([^\s\w]|_)+', '', document)
如果文件是用波兰语等语言编写的,我也会用这种方式删除许多(重音/特殊)字母
如何在一个表达式中删除上面的每个特殊字符?你知道吗
您可以通过使用^{}在不使用正则表达式的情况下解决此问题:
symbols = {"`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "{", "[", "]", "}", "|", "\\", ":", ";", "\"", "<", ",", ">", ".", "?", "/"} for c in symbols: document = document.replace(c, "")
symbols = ['a', 'b', '|'] document = document.translate({ord(c):None for c in symbols})
如果您想逐字删除每个字符,可以使用str.replace和string模块:
str.replace
a = '345l,we.gm34mf,]-=' for char in string.punctuation: a = a.replace(char, '') a
'345lwegm34mf'
如果需要更多的符号来替换(string.punctuation等于'!"#$%&\'()*+,-./:;<=>?@[\\]^_{|}~'), you can add them to ain`string。你知道吗
string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_
), you can add them to a
您可以通过使用^{} 在不使用正则表达式的情况下解决此问题:
如果您想逐字删除每个字符,可以使用
str.replace
和string模块:如果需要更多的符号来替换(
string.punctuation
等于'!"#$%&\'()*+,-./:;<=>?@[\\]^_
{|}~'), you can add them to a
in`string。你知道吗相关问题 更多 >
编程相关推荐