如何从.text中删除单词

时间:2011-06-29 22:08:00

标签: python

我正在读取文件.txt,现在我想删除重复的单词。

c = collections.Counter()
with open('DatoSO.txt', 'rt') as f:
        for line in f:
            c.update(line.split())


for palabra,count in c.most_common():
   if count > 1 :
       with open('DatoSO.txt', 'rt') as f:
            Here REMOVE

我不知道如何从文件中删除单词

3 个答案:

答案 0 :(得分:2)

您无法从文件中删除内容并将剩余内容向下移动。您只能追加,截断或覆盖。

您最好的选择是将文件读入内存,在内存中处理,然后将其写回磁盘。

答案 1 :(得分:0)

使用正则表达式:

import re

...

f = re.sub(r'\w+\s?','',f)

答案 2 :(得分:0)

不要使用重新

line.remove(string)