如何分割一行,其中单词仅由多个空格分隔

时间:2018-02-08 12:43:30

标签: python

location          severity       group           timestamp           description
'0/PM0             major         environ         02/07/18 22:50:55   Power Module Output Disabled'
'0/FT1             critical      environ         02/07/18 22:50:55   Fan tray is removed from chassis.'

从上面的输出中,我必须得到以location为键,severitygrouptimestampdescription作为值的字典。< / p>

我无法获得timestampdescription,请参阅输出:

{'0/PM0': ['major', 'environ', '22:50:55', 'Power'], '0/FT1': ['critical', 'environ', '22:50:55', 'Fan']}

预期输出:

{'0/PM0': ['major', 'environ', '02/07/18 22:50:55', 'Power Module Output Disabled'], '0/FT1': ['critical', 'environ', '02/07/18 22:50:55', 'Fan tray is removed from chassis']

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助:

import re
a = '''location          severity       group           timestamp           description
'0/PM0             major         environ         02/07/18 22:50:55   Power Module Output Disabled'
'0/FT1             critical      environ         02/07/18 22:50:55   Fan tray is removed from chassis.'
'''
d = {}
for i in a.strip().replace("'", "").split("\n")[1:]:
    val = re.split("\s\s+",i)
    d[val[0]] = val[1:]

print d['0/PM0'] 

<强>输出:

['major', 'environ', '02/07/18 22:50:55', 'Power Module Output Disabled']
相关问题