从另一个文件写入文件时,是否可以使用python跳过文本块?
例如,假设输入文件为:
This is the file I would like to write this line
I would like to skip this line
and this one...
and this one...
and this one...
but I want to write this one
and this one...
如何编写一个脚本,使我可以跳过内容和大小不同的某些行,一旦识别出某一行,就可以继续将行写到另一个文件中?
编辑
我的代码读取这些行,不编写重复的行,并使用字典和正则表达式对该行执行一些操作。
python大神给出的解决方案
def is_wanted(line):
#
# You have to define this!
#
# return True to keep the line, or False to discard it
def copy_some_lines(infname, outfname, wanted_fn=is_wanted):
with open(infname) as inf, open(outfname, "w") as outf:
outf.writelines(line for line in inf if wanted_fn(line))
copy_some_lines("file_a.txt", "some_of_a.txt")
为了将其扩展到多行块,您可以实现一个有限状态机,例如
会变成类似
class BlockState:
GOOD_BLOCK = True
BAD_BLOCK = False
def __init__(self):
self.state = self.GOOD_BLOCK
def is_bad(self, line):
# *** Implement this! ***
# return True if line is bad
def is_good(self, line):
# *** Implement this! ***
# return True if line is good
def __call__(self, line):
if self.state == self.GOOD_BLOCK:
if self.is_bad(line):
self.state = self.BAD_BLOCK
else:
if self.is_good(line):
self.state = self.GOOD_BLOCK
return self.state
然后
copy_some_lines("file_a.txt", "some_of_a.txt", BlockState())