python文件编程

2024-10-03 21:28:37 发布

您现在位置:Python中文网/ 问答频道 /正文

read_file = open ('C:\Users\Mahya\Desktop\\automate\Autosupports\\at1.txt','r')
content = read_file.readlines()
for line in content:
    if line.contains('===== BOOT TIME STATS ====='):
       print found

我想读'===== BOOT TIME STATS ====='这一行,把下面的行打印到下一行 请帮忙


Tags: txtreadtimestatslineautomateopencontent
3条回答
file = open ('C:\Users\Mahya\Desktop\\automate\Autosupports\\at1.txt','r')
for line in file:
    if '===== BOOT TIME STATS =====' in line: break
for line in file:
    if 'i wanna stop here' in line: break
    print line

未经测试:

read_file = open ('C:\Users\Mahya\Desktop\\automate\Autosupports\\at1.txt','r')
content = read_file.readlines()
i_found_the_block = False
for line in content:
    if "===== BOOT TIME STATS =====" in line:
       print ("found")
       i_found_the_block = True
    if i_found_the_block:
       print line

我猜您希望打印给定字符串第一次和第二次出现之间的行:

read_file = open ('C:\Users\Mahya\Desktop\\automate\Autosupports\\at1.txt','r')
content = read_file.readlines()
found = False
for line in content:
    if line.contains('===== BOOT TIME STATS ====='):
        if found:
            break # Exit the for loop when finding the string for the second time
        found = True
    if found:
        print line

相关问题 更多 >