Python - 从文本

时间:2016-08-27 06:09:22

标签: python

我希望Python只从字符串中删除一些标点符号,让我们说除了' @'

之外我想删除所有标点符号
import string
remove = dict.fromkeys(map(ord, '\n ' + string.punctuation))
sample = 'The quick brown fox, like, totally jumped, @man!'
sample.translate(remove)

这里的输出是

The quick brown fox like totally jumped man

但我想要的是这样的

The quick brown fox like totally jumped @man

有没有办法有选择地从文本中删除标点符号,从而忽略文本中我们想要的标点符号?

2 个答案:

答案 0 :(得分:3)

str.punctuation包含所有标点符号。从中删除@。每当你得到标点字符串时,用''替换。

>>> import re
>>> a = string.punctuation.replace('@','')
>>> re.sub(r'[{}]'.format(a),'','The quick brown fox, like, totally jumped, @man!')
'The quick brown fox like totally jumped @man'

答案 1 :(得分:3)

只需从替换字符串中删除您不想触摸的字符:

import string
remove = dict.fromkeys(map(ord, '\n' + string.punctuation.replace('@','')))
sample = 'The quick brown fox, like, totally jumped, @man!'
sample.translate(remove)

另请注意,我已将'\n '更改为'\n',因为前者会从字符串中删除空格。

结果:

The quick brown fox like totally jumped @man