python - 从特定行读取和写入文件
我不是在说具体的行号,因为我在读多个格式相同但长度不同的文件。
假设我有这个文本文件:
Something here...
... ... ...
Start #I want this block of text
a b c d e f g
h i j k l m n
End #until this line of the file
something here...
... ... ...
我希望你能明白我的意思。我在想要遍历这个文件,然后用正则表达式来查找“开始”和“结束”的行号,然后用linecache从开始行读到结束行。
但是,怎么才能得到行号呢?我可以用什么函数呢?
4 个回答
3
你可以很简单地使用正则表达式(regex)。如果需要的话,你可以让它更强大,下面是一个简单的例子。
>>> import re
>>> START = "some"
>>> END = "Hello"
>>> test = "this is some\nsample text\nthat has the\nwords Hello World\n"
>>> m = re.compile(r'%s.*?%s' % (START,END), re.S)
>>> m.search(test).group(0)
'some\nsample text\nthat has the\nwords Hello'
5
这里有一个可以用的东西:
data_file = open("test.txt")
block = ""
found = False
for line in data_file:
if found:
block += line
if line.strip() == "End": break
else:
if line.strip() == "Start":
found = True
block = "Start"
data_file.close()
37
如果你只是想获取“开始”和“结束”之间的那段文字,你可以简单地这样做:
with open('test.txt') as input_data:
# Skips text before the beginning of the interesting block:
for line in input_data:
if line.strip() == 'Start': # Or whatever test is needed
break
# Reads text until the end of the block:
for line in input_data: # This keeps reading the file
if line.strip() == 'End':
break
print line # Line is extracted (or block_of_lines.append(line), etc.)
实际上,你不需要去处理行号就能读取“开始”和“结束”标记之间的数据。
这里的逻辑(“读取直到……”)在两个代码块中都是重复的,但这样做非常清晰和高效(其他方法通常需要检查一些状态,比如在块之前、块内或块结束时,这样会浪费时间)。