【问题标题】:how to split a line in which words are separated by more than one space only [duplicate]如何拆分单词仅由多个空格分隔的行[重复]
【发布时间】:2018-02-08 12:43:30
【问题描述】:
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 作为值的字典。

我无法获取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']

【问题讨论】:

  • 使用正则表达式re.split("\s\s+",line)
  • 您要求我们想象您编写的代码,然后告诉您问题出在哪里。您是使用pandascsv 还是手动拆分输入?

标签: python


【解决方案1】:

这可能会有所帮助:

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']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-09
    • 2010-12-30
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多