![](/img/trans.png)
[英]parsing text file in python and creating dictionary of key value pair where values are in list format
[英]Parsing Text file having repeated value key pair using python
我有一个文本文件,其样式如下。我想解析每个“ Alert实例”的值,并将这些值分配给变量number,type,source等。实现此目的的选项
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
这是一种方法。
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']
------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.