pyt*_*arn 9 python regex string file string-formatting
我有一个庞大的文本语料库(逐行),我想删除特殊字符,但维持字符串的空间和结构.
hello? there A-Z-R_T(,**), world, welcome to python.
this **should? the next line#followed- by@ an#other %million^ %%like $this.
Run Code Online (Sandbox Code Playgroud)
应该
hello there A Z R T world welcome to python
this should be the next line followed by another million like this
Run Code Online (Sandbox Code Playgroud)
Chi*_*xus 14
您也可以使用此模式regex:
import re
a = '''hello? there A-Z-R_T(,**), world, welcome to python.
this **should? the next line#followed- by@ an#other %million^ %%like $this.'''
for k in a.split("\n"):
print(re.sub(r"[^a-zA-Z0-9]+", ' ', k))
# Or:
# final = " ".join(re.findall(r"[a-zA-Z0-9]+", k))
# print(final)
Run Code Online (Sandbox Code Playgroud)
输出:
hello there A Z R T world welcome to python
this should the next line followed by an other million like this
Run Code Online (Sandbox Code Playgroud)
编辑:
否则,您可以将最后一行存储到list:
final = [re.sub(r"[^a-zA-Z0-9]+", ' ', k) for k in a.split("\n")]
print(final)
Run Code Online (Sandbox Code Playgroud)
输出:
['hello there A Z R T world welcome to python ', 'this should the next line followed by an other million like this ']
Run Code Online (Sandbox Code Playgroud)
我认为 nfn neil 的答案很棒……但我只想添加一个简单的正则表达式来删除所有没有单词的字符,但是它会将下划线视为单词的一部分
print re.sub(r'\W+', ' ', string)
>>> hello there A Z R_T world welcome to python
Run Code Online (Sandbox Code Playgroud)
小智 6
你可以试试这个
import re
sentance = '''hello? there A-Z-R_T(,**), world, welcome to python. this **should? the next line#followed- by@ an#other %million^ %%like $this.'''
res = re.sub('[!,*)@#%(&$_?.^]', '', sentance)
print(res)
Run Code Online (Sandbox Code Playgroud)
re.sub('["]') -> 在这里您可以添加要删除的符号