简体   繁体   中英

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)?

您可以使用shlex.split函数保留引号内的空格。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM