【Python 基础】读取具有特定特征的行的例子

返回: Python基础 索引页

假设我的 程序是要读取如下类型的文件:

###
[Action --1]

RDMA support
IPOIB support
RDSOIB support

###<<<

###
[Action --2]

ORA-07445 
pevm_icd_call_common
ORA 7445 [pevm_icd_call_common]
oracle.sysman.oui.patch.PatchException: java.io.FileNotFoundException: 
ContentsXML/oui-patch.xml (Permission denied)

opatch logs

###<<<

我想找出每一个 "###" 之后的第一行,把它们汇集到一个列表里,
再都打印出来。

程序如下:

mylist = []

myfile = open('AI.txt')
line = myfile.readline()

while line:    
    current_line = line.strip()
    if ( current_line == "###" ) :  # read the next line when encounter ###
        line = myfile.readline()
        current_line = line.strip()
        mylist.append(current_line)
    line = myfile.readline() # to move to the next line

myfile.close()

for temp in mylist:
   print(temp)

运行结果如下:

[Action --1]
[Action --2]

 

返回: Python基础 索引页

 

posted @ 2022-03-06 15:30  健哥的数据花园  阅读(51)  评论(0编辑  收藏  举报