I am looking at a log message in the following format
datetime log_message_type message_type server {json_string}
Thus, each line is separated by a space, always has the same fields for each line, and at the end it has a json line with different fields inside the json block.
I thought about it with a simple
with open('test.log', 'r') as f:
for x in f:
line = x.split()
datetime = line[0]
log_message_type = line[1]
message_type = line[2]
server = line[3]
json_string = line[4]
This would work, except for spaces in my json string, for example, something like this.
{ "foo" : "bar" }
So by doing this, we would split my json string into spaces. Is there a way that I could use a regular expression or split something into spaces only until I get to the json string of the string and then save the rest? I tried to do something like
line = re.compile(".*\s.*\s.*\s.*\s").split(x)
4 json, , , regex python. - ?
: , python 2.7 .