#!/usr/bin/env python
with open("infile.txt") as infile:
with open("outfile.txt","w") as outfile:
collector = []
for line in infile:
if line.startswith("----"):
collector = []
collector.append(line)
if line.startswith("extractme"):
for outline in collector:
outfile.write(outline)
Python3号
#!/usr/bin/env python3
with open("infile.txt") as infile, open("outfile.txt","w") as outfile:
collector = []
for line in infile:
if line.startswith("----"):
collector = []
collector.append(line)
if line.startswith("extractme"):
for outline in collector:
outfile.write(outline)
def readbyblock(f):
while True:
block = []
for line in f:
if line = '----\n': break
block.append(line)
if not block: break
yield block
以便(选择性)输出可以与输入整齐地分开:
with open('infile.txt') as fin:
with open('oufile.txt', 'w') as fou:
for block in readbyblock(fin):
if 'extractme\n' in block:
fou.writelines(block)
fou.write('----\n')
def selectivereadbyblock(f, marker='extractme\n'):
while True:
block = []
extract = False
for line in f:
if line = '----\n': break
block.append(line)
if line==marker: extract = True
if not block: break
if extract: yield block
with open('infile.txt') as fin:
with open('oufile.txt', 'w') as fou:
for block in selectivereadbyblock(fin):
fou.writelines(block)
fou.write('----\n')
inFile = open("data.txt")
outFile = open("result.txt", "w")
buffer = []
keepCurrentSet = True
for line in inFile:
buffer.append(line)
if line.startswith("----"):
#---- starts a new data set
if keepCurrentSet:
outFile.write("".join(buffer))
#now reset our state
keepCurrentSet = False
buffer = []
elif line.startswith("extractme"):
keepCurrentSet = True
inFile.close()
outFile.close()
对于Python2
Python3号
我认为破折号数量的变化(输入中有4个破折号,输出中有时有4个,有时有3个)是一个错误,并不是实际需要的(因为甚至没有暗示任何算法,以解释在不同情况下要输出多少破折号)。
我将按照阅读和一次生成一段行的方式来组织任务:
以便(选择性)输出可以与输入整齐地分开:
如果块很大,这就不是最佳性能,因为它在
if
子句中隐含的块中的所有行上都有一个单独的循环。因此,一个好的重构可能是:参数化分隔符(对于输入和输出,现在硬编码为“---\n”)是另一个合理的编码调整。
这对我来说足够了。示例数据位于名为“data.txt”的文件中,输出转到“result.txt”
相关问题 更多 >
编程相关推荐