Matthew C
Matthew C

Reputation: 98

Converting log file line to dictionary

I have a log file generated by an external program I can't control that consists of a key-value pairs separated by spaces and I can't find a simple way to parse this. For example, a line would contain something like

time="2017-10-03T15:13:34Z" level=info msg="Some information message" 
time="2017-10-03T15:13:35Z" level=warn msg="Some basic message" err="More details on error"

I can't split on spaces because of the strings and I'm not entirely sure how to deal with this using regex because not everything is bounded in quotes.

Is there a simple way to convert a single line into a dictionary (or JSON)?

Upvotes: 0

Views: 188

Answers (1)

blue note
blue note

Reputation: 29081

You can use the shlex.split function to preserves spaces inside quotes.

Upvotes: 1

Related Questions