【问题标题】:Create Json from Raw Data from WSS Symantec?从 WSS 赛门铁克的原始数据创建 Json?
【发布时间】:2019-11-01 00:18:06
【问题描述】:

我需要来自 Symantec WSS 的过滤字符串数据, 我只有没有键的值。 所以我想为自己尝试行动并分割每个空间。

原始数据示例:

9777 10/30/2019 08:10:10 192.168.1.2 "Virus Found" Scott Sampson

我想要 JSON 的结果:

{
"pid":"9777",
"timestamp":"10/30/2019 08:10:10",
"ip":"192.168.1.2",
"message":"Virus Found",
"first_name":"Scott",
"first_name":"Sampson"
}

我启动了我的代码,但我卡住了:

data = r'''9777 10/30/2019 08:10:10 192.168.1.2 "Virus Found" Scott Sampson'''
ls1 = []
text = ""
for x in data:
    if x is '"':
        ls1.append('"')
    else:
        ls1.append(x)
print(ls1)

【问题讨论】:

    标签: python json regex python-3.x filter


    【解决方案1】:

    试试正则表达式。使用命名组,您可以直接从匹配对象中获取字典。

    import json
    import re
    
    pattern = re.compile(
        r'(?P<pid>\d+)\s(?P<timestamp>\d{1,2}\/\d{1,2}\/\d{4}\s\d{2}:\d{2}:\d{2})\s(?P<ip>(?:\d+\.?)+)\s\"(?P<message>('
        r'?:\w+\s?)+)\"\s(?P<first_name>\w+)\s(?P<last_name>\w+)')
    match = pattern.match(r'''9777 10/30/2019 08:10:10 192.168.1.2 "Virus Found" Scott Sampson''')
    print(json.dumps(match.groupdict()))
    

    regex101 example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 2011-05-13
      相关资源
      最近更新 更多