Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python读/写_Python_Sorting_File Io - Fatal编程技术网

Python读/写

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

我的程序必须读入一个有许多行的文本文件。然后它复制 输出文件中的文本相同,只是删除了所有无用的单词,如“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.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')