我正在使用python编写日志文件,日志文件如下所示:
MessageName:mouse left down | TimeStamp:2019-02-13 15:43:31.664 | Window:13500784 | Position:(483, 587) | Wheel:0
MessageName:mouse left up | TimeStamp:2019-02-13 15:43:31.873 | Window:13500784 | Position:(483, 587) | Wheel:0
我想将此日志转换为json格式。
这是我尝试过的代码:
import json
def convert() :
f = open("log_events.log", "r")
content = f.read()
splitcontent = content.splitlines()
for line in splitcontent :
pipesplit = line.split(' | ')
print(pipesplit)
with open("json_log.json", 'a') as fout:
json.dump(pipesplit, fout, indent=4)
我需要的输出是这样的:
[
{
"MessageName" : "mouse left down",
"TimeStamp" : "2019-02-13 15:43:31.664",
"Window" : "13500784",
"Position" : "(483, 587)",
"Wheel" : 0"
},
{
"MessageName" : "mouse left up",
"TimeStamp" : "2019-02-13 15:43:31.873",
"Window" : "13500784",
"Position" : "(483, 587)",
"Wheel" : "0"
},
]
但是使用上面给出的代码,我的输出是
[
"MessageName" : "mouse left down",
"TimeStamp" : "2019-02-13 15:43:31.664",
"Window" : "13500784",
"Position" : "(483, 587)",
"Wheel" : 0"
][
"MessageName" : "mouse left up",
"TimeStamp" : "2019-02-13 15:43:31.873",
"Window" : "13500784",
"Position" : "(483, 587)",
"Wheel" : "0"
]
如何转换为正确的JSON格式?
答案 0 :(得分:0)
您可以使用简单的split
设置和解析,如下所示:)
$ cat file.txt
MessageName:mouse left down | TimeStamp:2019-02-13 15:43:31.664 | Window:13500784 | Position:(483, 587) | Wheel:0
MessageName:mouse left up | TimeStamp:2019-02-13 15:43:31.873 | Window:13500784 | Position:(483, 587) | Wheel:0
# cat mkdict.py
import json
with open('file.txt') as file, open('dump.json', 'w') as json_file:
items = []
for line in file:
if not line.strip():
continue
d = {}
data = line.split('|')
for val in data:
key, sep, value = val.partition(':')
d[key.strip()] = value.strip()
items.append(d)
json.dump(items, json_file)
print(items)
$ python mkdict.py
[{'Wheel': '0', 'TimeStamp': '2019-02-13 15:43:31.664', 'Window': '13500784', 'Position': '(483, 587)', 'MessageName': 'mouse left down'}, {'Wheel': '0', 'TimeStamp': '2019-02-13 15:43:31.873', 'Window': '13500784', 'Position': '(483, 587)', 'MessageName': 'mouse left up'}]
$ cat dump.json
[{"Wheel": "0", "TimeStamp": "2019-02-13 15:43:31.664", "Window": "13500784", "Position": "(483, 587)", "MessageName": "mouse left down"}, {"Wheel": "0", "TimeStamp": "2019-02-13 15:43:31.873", "Window": "13500784", "Position": "(483, 587)", "MessageName": "mouse left up"}]
答案 1 :(得分:0)
您需要为每行添加额外的处理,将字符串转换为key:value对,然后将该对添加到字典中。
此外,您只需要打开JSON文件一次并编写整个数据结构:
import json
def line_to_dict(split_Line):
# Assumes that the first ':' in a line
# is always the key:value separator
line_dict = {}
for part in split_Line:
key, value = part.split(":", maxsplit=1)
line_dict[key] = value
return line_dict
def convert() :
f = open("log_events.log", "r")
content = f.read()
splitcontent = content.splitlines()
# Split each line by pipe
lines = [line.split(' | ') for line in splitcontent]
# Convert each line to dict
lines = [line_to_dict(l) for l in lines]
# Output JSON
with open("json_log.json", 'w') as fout:
json.dump(lines, fout, indent=4)
答案 2 :(得分:0)
以下代码应与列表和字典配合使用
import json
logfile = open('log_events.log')
#initialising a list to append all the log lines formatted as json
log_list = []
for line in logfile:
# splitting on '|'
pipe_split = [ele.strip() for ele in line.split("|")]
# initialising dictionary to fill the line splitted data in key-value pairs
line_dict = dict()
for ele in pipe_split:
# splitting on first occurrence of ':'
key,val = ele.split(":",1)
line_dict[key] = val
# appending the key-value data of each line to a list
log_list.append(line_dict)
with open('json_log.json','w') as f:
json.dump(log_list,f,indent=4)
以以下格式输出到文件,
[
{
"MessageName": "mouse left down",
"TimeStamp": "2019-02-13 15:43:31.664",
"Window": "13500784",
"Position": "(483, 587)",
"Wheel": "0"
},
{
"MessageName": "mouse left up",
"TimeStamp": "2019-02-13 15:43:31.873",
"Window": "13500784",
"Position": "(483, 587)",
"Wheel": "0"
}
]
答案 3 :(得分:0)
您最正确的地方。您没有做的是生成dict
。
split
首次出现:
dump
将它们放入您的.json
。魔术在这一行:
d.append(dict(s.split(':',1) for s in l))
用词来表示:将:
中第一次出现的每个元素拆分为l
中的每个元素,然后将它们放入dict,然后将它们附加到列表d
工作示例:
import json
f = open("log.file", "r")
content = f.read()
splitcontent = content.splitlines()
d = []
for v in splitcontent:
l = v.split(' | ')
d.append(dict(s.split(':',1) for s in l))
with open("json_log.json", 'w') as file:
file.write((json.dumps(d, indent=4, sort_keys= False)))
输出:
[
{
"MessageName": "mouse left down",
"TimeStamp": "2019-02-13 15:43:31.664",
" Window": "13500784",
"Position": "(483, 587)",
"Wheel": "0"
},
{
"MessageName": "mouse left up",
"TimeStamp": "2019-02-13 15:43:31.873",
"Window": "13500784",
"Position": "(483, 587)",
"Wheel": "0"
}
]