Python-从字符串中删除特殊字符

时间:2018-07-25 07:12:51

标签: python regex python-3.x

我在下面显示此文本,并尝试使用Python删除图像中显示的特殊字符。

enter image description here

更新(粘贴文本):

145,Kevin,07/06/2018 15:12:37,Kevin,nan,nan,"have to clear outstanding tasks. 
check schedule "

我尝试了以下方法,但是没有运气

DF['col'] = re.sub("[^a-zA-Z]", " ", str(DF['col']))

任何人都可以提供协助。谢谢。

3 个答案:

答案 0 :(得分:0)

我认为您只是错过了+

尝试

DF['col'] = re.sub("[^A-Za-z0-9]+", " ", str(DF['col']))

答案 1 :(得分:0)

import re,string
s='145,Kevin,07/06/2018 15:12:37,Kevin,nan,nan,"have to clear outstanding tasks. ♔ \ncheck schedule '
punc=re.escape(string.punctuation)
re.sub(fr"[^\w\s{punc}]","",s)
Out:
'145,Kevin,07/06/2018 15:12:37,Kevin,nan,nan,"have to clear outstanding tasks.  \ncheck schedule '

答案 2 :(得分:0)

df['col'] = re.sub('[^A-Za-z0-9]+',' ',str(df['col'])

如果要将功能应用于所有行,则可以执行以下操作:

df['col'] = df['col'].map(lambda x: re.sub('[^A-Za-z0-9]+',' ',str(df['col'])))