简体   繁体   English

从一个文件读到另一个缺少某些行

[英]read from one file to another missing certain lines

I have a text file (file1.txt) with 1 string per line over many lines. 我有一个文本文件(file1.txt),每行多行包含1个字符串。 I'm trying to read in the file and write certain lines to a new file.(file2.txt) 我正在尝试读取文件并将某些行写入新文件。(file2.txt)

My text file looks something like this. 我的文本文件看起来像这样。

foo1
foo2
foo3
foo4
foo5
foo6

etc.. 等等..

for example, i want to write foo1,foo2,foo4,foo6 to my new file and miss out foo3 and foo5. 例如,我想将foo1,foo2,foo4,foo6写入新文件,而错过foo3和foo5。

foo1
foo2
foo4
foo6

I wish to preserve the original file. 我希望保留原始文件。

My code looks like this... 我的代码看起来像这样...

with open("file1.txt","r") as r:
    lines=r.read()
    lines =lines.replace("foo3","")
    lines = lines.replace("foo5","")

r.close()
with open("file2.txt","a") as w:
    w.write(lines)
w.close

The problem is I end up with this output.. 问题是我最终得到此输出。

foo1
foo2

foo4

foo6

I think this is because i am replacing foo with "" how do I get rid of the white space? 我认为这是因为我将foo替换为“”,如何摆脱空白?

TIA, TIA,

Paul. 保罗。

The minimal change is to also replace the line separators by changing the replace calls to: 最小的更改是通过将replace调用更改为来替换行分隔符:

lines =lines.replace("foo3\n","")
lines = lines.replace("foo5\n","")

Presuming that the exclusions are variable: 假定排除项是可变的:

def rwfile(infile, outfile, exceptions=[]):

    o = open(outfile, "w")

    for line in open(infile):
        if line.rstrip() not in exceptions:
            o.write(line)

    o.close()


rwfile("in", "out", ['foo3', 'foo5'])

In: 在:

foo1
foo2
foo3
foo4
foo5
foo6
foo7
foo8
foo9

Out: 日期:

foo1
foo2
foo4
foo6
foo7
foo8
foo9

Following on from the OP's comments - here's a version using a predicate function to decide which lines should be included. 根据OP的注释-这是一个使用谓词功能来确定应包含哪些行的版本。

def rwfilep(infile, outfile, predicate=lambda x: True):

    o = open(outfile, "w")

    for line in open(infile):
        if predicate(line):
            o.write(line)

    o.close()

def ignore_some(line):
    """return True to include"""
    return line.rstrip() not in ['foo3', 'foo5']

def ignore_comments(line):
    """return True to include"""
    return not line.startswith("#")

rwfilep("in", "out2", ignore_some)

rwfilep("in", "out3", ignore_comments)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM