我有一个大文件,我需要重复解析数据并将其输出到新文件。以下是原始文件中某些内容的示例:
<example>
type : FILE_CONTENT_CHECK
description : "This is a description"
info : "Info!"
info : ""
info : "References:"
info : "book1, book 2, etc"
file : "blahblah.txt"
</example>
我需要能够在:之后提取描述和所有信息行,并将它们放在一个新文件中。请记住,在同一个文件中有几个这样的实例,我希望能够抓取它们并将它们导出到一个新文件。
f = open("test.txt", "r")
searchlines = f.readlines()
f.close()
for i, line in enumerate(searchlines):
if "description" in line:
for l in searchlines[i:i+1]: print l,
print
if "info" in line:
for l in searchlines[i:i+1]: print l,
print
我用它来打印整行,但想输出如下内容:
"This is a description!"
"Info!"
""
"References:"
"book1, book 2, etc"
答案 0 :(得分:0)
我建议使用&#39; printme&#39;旗。如果设置了标志,则打印行的值并关闭标志。标记将设置为&#39;描述&#39;或者&#39; info&#39;关键。
EXAMPLE = '''
type : FILE_CONTENT_CHECK
description : "This is a description"
info : "Info!"
info : ""
info : "References:"
info : "book1, book 2, etc"
file : "blahblah.txt"
'''
do_print = False
for line in EXAMPLE.split('\n'):
if ':' not in line:
continue
key,value = line.split(':',1)
key = key.strip()
value = value.strip()
if key in ('description', 'info'):
print value
do_print = True
continue
if do_print:
print value
do_print = False
"This is a description"
"Info!"
""
"References:"
"book1, book 2, etc"
"blahblah.txt"