开头。
如果我有一个这样的文本文件:
[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:
...
现在我需要合并当前行和下一行。找到((太长))> = 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()
,但它没有工作。为什么?
听起来开销太大,需要额外读取文件。试试这个:
with open('file.txt') as f:
for line in f:
if '[too long]' in line:
line = line.rstrip('rn') + 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]
?
您可以使用列表推导将所有行放到一个列表中,这与eumiros的答案非常相似。
with open('file.txt') as f:
lines = [line.rstrip('rn') + 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']
我不确定实际文件的样子,但我可能会这样做:
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