【发布时间】:2022-01-19 00:52:15
【问题描述】:
在我需要的数据之前,我有一个包含大量调试信息的文本文件。我正在使用 python3 尝试重写输出,因此文件以特定的 JSON 标记开头。我试图使用这个解决方案Remove string and all lines before string from file,但我得到一个空的输出文件,所以我认为它没有找到 JSON 标记。
这是我的代码:
tag = '"meta": ['
tag_found = False
with open('file.in',encoding="utf8") as in_file:
with open('file.out','w',encoding="utf8") as out_file:
for line in in_file:
if tag_found:
if line.strip() == tag:
tag_found = True
else:
out_file.write(line)
【问题讨论】:
标签: python python-3.x line