Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—如何在具有特定值的两行之间逐行读取文件_Python_Xml_File Io - Fatal编程技术网

Python—如何在具有特定值的两行之间逐行读取文件

Python—如何在具有特定值的两行之间逐行读取文件,python,xml,file-io,Python,Xml,File Io,我正在开发一个Python程序,它将获取另一个程序生成的日志文件(XML),并从中读取数据,提取我想要的信息,然后将其打印到控制台/写入另一个文件 我正在读取的日志文件包含以特定格式写入的数据(许多日志),例如: 2017-05-01 00:00:24 [Client/Report Report] INFO {'message-id': 'ID:ELX-LVL-ETL01-50079-1493596823959-1:1:1:1:1', 'expires': '1493640024119'

我正在开发一个Python程序,它将获取另一个程序生成的日志文件(XML),并从中读取数据,提取我想要的信息,然后将其打印到控制台/写入另一个文件

我正在读取的日志文件包含以特定格式写入的数据(许多日志),例如:

2017-05-01 00:00:24 [Client/Report Report] INFO     {'message-id': 'ID:ELX-LVL-ETL01-50079-1493596823959-1:1:1:1:1', 'expires': '1493640024119', 'type': 'MID', 'persistent': 'true', 'destination': '/topic/bmrsTopic', 'timestamp': '1493596824119', 'subscription': 'TtJXJBbtEOcpLrjDsbFD', 'priority': '4'}
<?xml version="1.0" encoding="utf-8"?>
<msgGrp>
    <pubTs>2017-05-01 00:00:24:GMT</pubTs>
    <flow>MID</flow>
<msg>
    <subject>BMRA.SYSTEM.MID</subject>
<row>
    <MI>APXMIDP</MI>
    <SD>2017-05-01 00:00:00:GMT</SD>
    <SP>2</SP>
    <M1>30.620</M1>
    <M2>456.3000</M2>
</row>
</msg>
</msgGrp>

2017-05-01 00:00:39 [Client/Report Report] INFO     {'message-id': 'ID:ELX-LVL-ETL01-59269-1493596838709-1:1:1:1:1', 'expires': '1493640039019', 'type': 'NONBM', 'persistent': 'true', 'destination': '/topic/bmrsTopic', 'timestamp': '1493596839019', 'subscription': 'TtJXJBbtEOcpLrjDsbFD', 'priority': '4'}
<?xml version="1.0" encoding="utf-8"?>
<msgGrp>
    <pubTs>2017-05-01 00:00:38:GMT</pubTs>
    <flow>NONBM</flow>
<msg>
    <subject>BMRA.SYSTEM.NONBM</subject>
    <TP>2017-05-01 00:00:00:GMT</TP>
<row>
    <SD>2017-05-01 00:00:00:GMT</SD>
    <SP>2</SP>
    <NB>0</NB>
</row>
</msg>
</msgGrp>

2017-05-01 00:00:41 [Client/Report  Report] INFO     {'message-id': 'ID:ELX-LVL-ETL01-35161-1493596840568-1:1:1:1:1', 'expires': '1493640040868', 'type': 'INDO', 'persistent': 'true', 'destination': '/topic/bmrsTopic', 'timestamp': '1493596840868', 'subscription': 'TtJXJBbtEOcpLrjDsbFD', 'priority': '4'}
<?xml version="1.0" encoding="utf-8"?>
<msgGrp>
    <pubTs>2017-05-01 00:00:40:GMT</pubTs>
    <flow>INDO</flow>
<msg>
    <subject>BMRA.SYSTEM.INDO</subject>
<row>
    <TP>2017-05-01 00:00:00:GMT</TP>
    <SD>2017-05-01 00:00:00:GMT</SD>
    <SP>2</SP>
    <VD>21627.000</VD>
</row>
</msg>
</msgGrp>
2017-05-01 00:00:24[客户/报告]信息{'message-id':'id:ELX-LVL-ETL01-50079-1493596823959-1:1:1:1:1','expires':'14936402119','type':'MID','persistent':'true','destination':'/topic/bmrsTopic','timestamp':'1493596824119','subscription':'TTJXJBEOCPLRJDSD','priority':'4'}
2017-05-01 00:00:24:GMT
中间
BMRA.SYSTEM.MID
APXMIDP
2017-05-01 00:00:00:GMT
2.
30.620
456.3000
2017-05-01 00:00:39[客户/报告]信息{'message-id':'id:ELX-LVL-ETL01-59269-149359683709-1:1:1:1','expires':'149364039019','type':'NONBM','persistent':'true','destination':'/topic/bmrsTopic','timestamp':'1493596839019','subscription':'ttjxjbeocplrjdsbfd','priority':'4'}
2017-05-01 00:00:38:GMT
非BM
BMRA.SYSTEM.NONBM
2017-05-01 00:00:00:GMT
2017-05-01 00:00:00:GMT
2.
0
2017-05-01 00:00:41[客户/报告]信息{'message-id':'id:ELX-LVL-ETL01-35161-1493596840568-1:1:1:1','expires':'149364040868','type':'INDO','persistent':'true','destination':'/topic/bmrsTopic','timestamp':'149356840868','subscription':'TTJJBBTEOCPLRJDSD','priority':'4'}
2017-05-01 00:00:40:GMT
印度语
BMRA.SYSTEM.INDO
2017-05-01 00:00:00:GMT
2017-05-01 00:00:00:GMT
2.
21627
我想读取每个
&
标记(即实际的XML)之间的所有信息,并将其附加到字符串中,然后将其转换为XML树

我已经开始编写Python脚本来实现这一点,并让它正确地读取一些信息。我现在想读取这两个XML标记之间的所有内容,并将它们附加到字符串中,但我不太明白如何做到这一点。。。我的Python程序目前如下所示:

with open("logFile") as file:
    for line in file:
        if(line.startswith('2017')):
            # Do something
            # Display some output in the console
        elif(line.startswith('<?xml')):
            # Do something else
            # Display some output in the console
        elif(line.startswith('<msgGrp>')):
            xmlString = "";
            while(not(line.startswith('</msgGrp>'))):
                xmlString += line;
            # Display some output in the console
        else:
            # Do something else
打开(“日志文件”)作为文件:
对于文件中的行:
如果(行开始('2017')):
#做点什么
#在控制台中显示一些输出
elif(行开始与('
请注意:

  • 串接字符串已经不好了,最好使用
    io.StringIO
    或list+join)
  • 使用适当的XML/HTML解析器并不困难,建议使用。解析类似XML/HTML的文本可能会在某个时候中断
请注意:

  • 串接字符串已经不好了,最好使用
    io.StringIO
    或list+join)
  • 使用适当的XML/HTML解析器并不困难,建议您这样做。解析类似XML/HTML的文本可能会在某个时候中断

谢谢您的回答。几个后续问题:为什么这是一个无限循环?有一行是以
开头的,再往下走几行-因此我希望
while
循环在到达该行时停止。我希望
while
循环从
开头的行开始,读取每一行,将该行附加到变量
xmlString
,直到它到达以“`”开头的行,此时它应该停止读取文件中的行
直接在
elif
中,我只是将
行反复添加到字符串中……感谢您的回答。几个后续问题:为什么这是一个无限循环?有一行是以
开头的,再往下走几行-因此我希望
while
循环在到达该行时停止。我希望
while
循环从
开头的行开始,读取每一行,将该行附加到变量
xmlString
,直到它到达以“`”开头的行,此时它应该停止读取文件中的行直接在
elif
中,我只是将行
一次又一次地附加到字符串中。。。
   while(not(line.startswith('</msgGrp>'))):
        xmlString += line;
    elif(line.startswith('<msgGrp>')):
        start = True
        xmlString = ""

    elif(line.startswith('</msgGrp>'))):
        start = False

    if start:
        xmlString += line