Python - 从列表中删除标点符号

时间:2012-09-17 05:34:58

标签: python python-3.x

我需要从文本文件中删除punc。

文本文件是这样的

  

ffff,hhhh,& tommorw home,
  你离开了吗?

我正在尝试

  

PUNC =(”。?,/;'&安培; - “)

     

f = open('file.txt','r')

for line in f:
    strp=line.replace(punc,"")
    print(strp)

我需要输出:

  

ffff hhhh tommorw home

Have you from gone

这是返回每一行,但是punc仍在那里>可以使用一些帮助。谢谢

4 个答案:

答案 0 :(得分:9)

使用str.translate删除字符串中的字符。

在Python 2.x中:

# first arg is translation table, second arg is characters to delete
strp = line.translate(None, punc)

在Python 3中:

# translation table maps code points to replacements, or None to delete
transtable = {ord(c): None for c in punc}
strp = line.translate(transtable)

或者,您可以使用str.maketrans构建transtable

# first and second arg are matching translated values, third arg (optional) is the characters to delete
transtable = str.maketrans('', '', punc)
strp = line.translate(transtable)

答案 1 :(得分:3)

>>> import string
>>> with open('/tmp/spam.txt') as f:
...   for line in f:
...     words = [x.strip(string.punctuation) for x in line.split()]
...     print ' '.join(w for w in words if w)
... 
ffff hhhh tommorw home
Have you from gone

答案 2 :(得分:0)

import string

str_link = open('replace.txt','r').read()

#str_link = "ffff, hhhh, & tommorow home, Have you from gone?"

punc = list(",./;'?&-")

for line in str_link:
    if line in punc:
        str_link = str_link.replace(line,"") 

print str_link

答案 3 :(得分:0)

我认为使用str.translate的想法很棒,但这是另一种方法:

punc=set(",./;'?&-")

for line in f:
    strp=''.join(c for c in line if not c in punc)
    print(strp)