python3取以Thread Name:1.1开始,一直到Thread Name:1.2中间的行
时间: 2024-03-20 20:43:24 浏览: 84
假设你有一个名为log.txt的文本文件,其中包含以下内容:
```
Thread Name:1.0 Started
Thread Name:1.0 Running
Thread Name:1.1 Started
Thread Name:1.1 Running
Thread Name:1.1 Completed
Thread Name:1.2 Started
Thread Name:1.2 Running
Thread Name:1.2 Completed
Thread Name:1.0 Completed
```
你可以使用以下代码来获取从"Thread Name:1.1 Started"到"Thread Name:1.2 Completed"之间的行:
```python
with open("log.txt", "r") as f:
started = False
for line in f:
if "Thread Name:1.1 Started" in line:
started = True
elif "Thread Name:1.2 Completed" in line:
break
elif started:
print(line.strip())
```
这将输出:
```
Thread Name:1.1 Running
Thread Name:1.1 Completed
Thread Name:1.2 Started
Thread Name:1.2 Running
```
阅读全文