Python读/写
我的程序必须读入一个有许多行的文本文件。然后它复制 输出文件中的文本相同,只是删除了所有无用的单词,如“the”、“a”和“an”。有什么问题Python读/写,python,sorting,file-io,Python,Sorting,File Io,我的程序必须读入一个有许多行的文本文件。然后它复制 输出文件中的文本相同,只是删除了所有无用的单词,如“the”、“a”和“an”。有什么问题 f=open("a.txt","r") inp=f.readlines() f.close() out=open("a.txt","w") stopList=['the','a','an'] for i in inp: if i in stopList: out.write(i) out.close() 给你,只需使用str.r
f=open("a.txt","r")
inp=f.readlines()
f.close()
out=open("a.txt","w")
stopList=['the','a','an']
for i in inp:
if i in stopList:
out.write(i)
out.close()
给你,只需使用
str.replace
:
with open("a.txt","r") as fin, open("b.txt","w") as fout:
stopList=['the','a','an']
for line in fin:
for useless in stopList:
line = line.replace(useless+' ', '')
fout.write(line)
如果不想将整个文件存储到内存中,则需要将结果写入其他地方。但如果你不介意,你可以重写它:
with open("a.txt","r") as fin, open("a.txt","w") as fout:
stopList=['the','a','an']
r = []
for line in fin:
for useless in stopList:
line = line.replace(useless+' ', '')
r.append(line)
fout.writelines(r)
演示:
使用:
“a.txt”
将包含首字母+附加行,因为您没有清除该文件。不知道这是否重要。此外,您能否告诉我们问题的症状是什么,即发生了什么而不是您希望发生什么?您的文件中有一个所有行的列表。您正在遍历列表,检查一行是否在只包含三个单词“the”、“a”、“an”的停止列表中。这里有点不对劲,你不觉得吗?@alKid它复制了三次一个元素你说的“三次一个元素”是什么意思?@alKid例如它写了三次“ABC”这个词,比如“ABC”哦,哇!很抱歉。更新。
>>> line = 'the a, the b, the c'
>>> stopList=['the','a','an']
>>> for useless in stopList:
line = line.replace(useless+' ', '')
>>> line
'a, b, c'
import re
with open('a.txt') as f, open('b.txt','w') as out:
stopList = ['the', 'a', 'an']
pattern = '|'.join(r'\b{}\s+'.format(re.escape(word)) for word in stopList)
pattern = re.compile(pattern, flags=re.I)
out.writelines(pattern.sub('', line) for line in f)
# import shutil
# shutil.move('b.txt', 'a.txt')