简体   繁体   English

将日志文件行转换为字典

[英]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)? 有没有一种简单的方法可以将单行转换为字典(或JSON)?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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