用python解析具有重复值密钥对的文本文件

2024-09-25 18:22:49 发布

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

我有一个文本文件,其模式如下..我想解析每个“警报实例”的值,并将值分配给变量编号、类型、源等。。。。。实现这一目标的最佳选择是什么

An instance of Alert
      number=2
      type=Server
      source=ARRAY.R800.56794
      severity=4
      component=DKC Environment
      description=Moderate error reported for DKC Environment.
      actionToTake=Contact your service representative.
      data=The component is not fully functional.
      timeOfAlert=2018/05/26 04:52:17
    An instance of Alert
      number=1
      type=Server
      source=ARRAY.R800.57457
      severity=4
      component=DKC Environment
      description=Moderate error reported for DKC Environment.
      actionToTake=Contact your service representative.
      data=The component is not fully functional.
      timeOfAlert=2018/05/26 04:07:42

def GetAlerts(serialNumber,arrayType):
    print("Collecting Alert Data")
    remove(HiCommandCLI_XML_Path+"GetAlerts.txt")
    sourcefilter ="ARRAY."+arrayType+"."+serialNumber
    cmd = "/san02/hds/OAC/HiCommandCLI GetAlerts sourcefilter=%s -o %s/GetAlerts.txt" % (sourcefilter,HiCommandCLI_XML_Path)
    os.system(cmd)

# Parse the TEST to the text file as "Component" "Identifier" "Status"    
#"Result" format
    xmlfile = HiCommandCLI_XML_Path+'/GetAlerts.txt'
    with open (xmlfile, "r") as alertfile:
        s=alertfile.readlines()
    for block in s.split("An instance of Alert"):
        for line in block.strip().splitlines(): 
            line = line.strip()
            if line:
                print(line.split("="))
        print("------")

期望输出(在变量中,因为我不想打印,但处理变量) 我会用每个块的其他变量做一些计算…所以基本上,我需要把每个块看作一个对象,提取数值,类型,源等等。。。从每个街区

    number=2 type=server source=ARRAY.R800.56794
    number=1 type=server source=ARRAY.R800.57457

Tags: ofinstanceannumbersourceforenvironmenttype
1条回答
网友
1楼 · 发布于 2024-09-25 18:22:49

这是一种方法。你知道吗

s = """An instance of Alert
      number=2
      type=Server
      source=ARRAY.R800.56794
      severity=4
      component=DKC Environment
      description=Moderate error reported for DKC Environment.
      actionToTake=Contact your service representative.
      data=The component is not fully functional.
      timeOfAlert=2018/05/26 04:52:17
An instance of Alert
      number=1
      type=Server
      source=ARRAY.R800.57457
      severity=4
      component=DKC Environment
      description=Moderate error reported for DKC Environment.
      actionToTake=Contact your service representative.
      data=The component is not fully functional.
      timeOfAlert=2018/05/26 04:07:42"""

for block in s.split("An instance of Alert"):    #use str.split on "An instance of Alert"
    for line in block.strip().splitlines():      #Iterate each line in block
        line = line.strip()
        if line:
            print(line.split("="))                #Get Value
     print("   ")

输出:

   
['number', '2']
['type', 'Server']
['source', 'ARRAY.R800.56794']
['severity', '4']
['component', 'DKC Environment']
['description', 'Moderate error reported for DKC Environment.']
['actionToTake', 'Contact your service representative.']
['data', 'The component is not fully functional.']
['timeOfAlert', '2018/05/26 04:52:17']
   
['number', '1']
['type', 'Server']
['source', 'ARRAY.R800.57457']
['severity', '4']
['component', 'DKC Environment']
['description', 'Moderate error reported for DKC Environment.']
['actionToTake', 'Contact your service representative.']
['data', 'The component is not fully functional.']
['timeOfAlert', '2018/05/26 04:07:42']
   

相关问题 更多 >