从WSS Symantec的原始数据创建Json?

2024-05-12 20:37:21 发布

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

我需要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)

Tags: 数据字符串namedata原始数据空间scottfirst
1条回答
网友
1楼 · 发布于 2024-05-12 20:37:21

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

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

相关问题 更多 >