我需要从文本文件中删除标点符号。
文本文件是这样的
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
这是返回每一行,但双关语仍然存在>可以使用一些帮助。谢谢
我需要从文本文件中删除标点符号。
文本文件是这样的
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
这是返回每一行,但双关语仍然存在>可以使用一些帮助。谢谢
用于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)
>>> 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
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
我认为使用的想法str.translate
很棒,但这是另一种方法:
punc=set(",./;'?&-")
for line in f:
strp=''.join(c for c in line if not c in punc)
print(strp)