I'm writing a log file using python and the log file is like this :
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
I would like to convert this log into json format.
This is the code that i have tried :
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)
And the Output i need is like this :
[
{
"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"
},
]
But with the above given code, my output is
[
"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"
]
How to convert into proper JSON format ?
You need to add extra handling for each line, to convert the string into a key:value pair, then add that pair to a dictionary.
Also, you only need to open the JSON file once and write the entire data structure:
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)
You could use simple splitting and parsing like the below :)
$ 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"}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With