我有以下文件:
>Line1
Some contents of line 1
>Line2
------Some contents I don't need
>Line3
More content of line 3
我尝试删除以---
开头的行以及前一行,结果如下:
>Line1
Some contents of line 1
>Line3
More content of line 3
这是我当前的代码,目前我只能删除以---
开头的行,但不能删除它前面的行:
with open("test.txt") as f1:
for lines in f1:
if lines.startswith("---"):
pass
else:
print(lines)
这里有一个建议:
这是我能想到的最具可读性的解决方案
您可以尝试此脚本不打印以
---
开头的行及其前面的行:相关问题 更多 >
编程相关推荐