2

我需要从文本文件中删除标点符号。

文本文件是这样的

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

我在尝试

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

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

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

我需要输出为:

ffff hhhh 明天回家

Have you from gone

这是返回每一行,但双关语仍然存在>可以使用一些帮助。谢谢

4

4 回答 4

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)
于 2012-09-17T05:37:11.963 回答
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
于 2012-09-17T05:37:10.930 回答
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
于 2013-05-04T09:37:40.183 回答
0

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

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

for line in f:
    strp=''.join(c for c in line if not c in punc)
    print(strp)
于 2013-05-04T10:55:21.553 回答