我需要从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)发布于 2019-11-01 15:04:38
尝试正则表达式。使用命名组,您可以直接从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()))https://stackoverflow.com/questions/58652685
复制相似问题