【发布时间】:2019-11-16 07:38:34
【问题描述】:
我需要从 txt 文件中提取一部分文本。
文件如下所示:
STARTINGWORKIN DD / MM / YYYY HH: MM: SS
... 文本行 ...
... 更多文本行 ...
开始工作 DD / MM / YYYY HH: MM: SS
... 我想要的文本行 ...
...更多我想要的文本行...
-
文件以 STARTINGWORK 开头并以文本行结尾。
我需要在最后一个 STARTINGWORK 之后提取最终文本部分,不带 STARTINGWORK str
我尝试使用 3 个 for 循环(一个开始,另一个读取中间行,最后一个结束)
file = "records.txt"
if file.endswith (".txt"):
if os.path.exists (file):
lines = [line.rstrip ('\ n') for line in open (file)]
for line in lines:
#extract the portion
【问题讨论】:
标签: python python-3.x file for-loop readlines