从文本文件中读取一行

时间:2013-01-02 12:07:24

标签: python file iterator

如果我有这样的文本文件:

[001]This is line 1.
[002][too long]This is line 2 but it's Tooooo
oooo long!
[003]This is line 3.

我写了一个'for line in fileA'来读取这个文件,如:

for line in fileA:
    ...

现在我需要在line.find(“[too long]”)> = 0时合并当前行和下一行。 我该怎么办?

PS: 我写道:

for line in fileA:
    if line.find("[too long]")>=0:
        loc = fileA.tell()
        fileB = open("file.txt") #open this file again
        fileB.seek(loc)
        line += fileB.readline().strip()

但它不起作用。为什么呢?

3 个答案:

答案 0 :(得分:3)

额外阅读文件会产生太多开销。试试这个:

with open('file.txt') as f:
    for line in f:
        if '[too long]' in line:
            line = line.rstrip('\r\n') + next(f)
        print line

打印

[001]This is line 1.

[002][too long]This is line 2 but it's Tooooooooo long!

[003]This is line 3.

如果在一行中找到[too long],则会附加以下行。也许你想要追加所有更多的行,直到一行开头像[xxx]

答案 1 :(得分:2)

您可以使用列表推导将列表中的所有行与eumiros答案非常相似。

with open('file.txt') as f:
    lines = [line.rstrip('\r\n') + next(f) if '[too long]' in line else line for line in f]

然后输出是:

>>> lines
    ['[001]This is line 1.\n', "[002][too long]This is line 2 but it's Tooooooooo long!\n", '[003]This is line 3.\n']

答案 2 :(得分:0)

我不确定实际文件是怎么样的,但我可能会选择像this这样的文件:

contents = """[001]This is line 1.
[002][too long]This is line 2 but it's Tooooo
oooo long!
[003]This is line 3.
"""

lines = iter( contents.split("\n") )

def fix_file( lines ):
    prev = ''
    number = 1
    for line in lines:
        if not line.startswith( '[{0:03d}]'.format( number ) ):
            prev += line
        else:
            yield prev
            number = number + 1
            prev = line
    yield prev

for line in fix_file( lines ):
    print line

这样您就不需要在行中添加额外内容了。