2013-01-02 314 views
1

虚线,如果我有一个文本文件是这样的:阅读从文本文件

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

我写了“为的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() 

,但没有奏效。为什么?

+0

您无法打开同一个文件,请张贴错误信息/堆栈跟踪,究竟是什么'didnt的工作?你不清楚你正在做什么,这是阻碍我们的帮助。 –

+1

迭代遍历行,维护一个缓冲区。当一行以'[...]开头时,产生并清除缓冲区的内容,然后追加新的内容。当一行不以'[...]开始时,将其追加到缓冲区。 – katrielalex

回答

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]之类的内容开头的行?

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 

这样,你不需要额外的行内容。

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']