gtf*_*tfx 6 python csv logging pyparsing
我有一个像下面这样的字符串:
<118>date=2010-05-09,time=16:41:27,device_id=FE-2KA3F09000049,log_id=0400147717,log_part=00,type=statistics,subtype=n/a,pri=information,session_id=o49CedRc021772,from="prvs=4745cd07e1=example@example.org",mailer="mta",client_name="example.org,[194.177.17.24]",resolved=OK,to="example@example.org",direction="in",message_length=6832079,virus="",disposition="Accept",classifier="Not,Spam",subject="=?windows-1255?B?Rlc6IEZ3OiDg5fDp5fog+fno5fog7Pf46eHp7S3u4+Tp7SE=?="
Run Code Online (Sandbox Code Playgroud)
我尝试使用CSV模块,它不适合,因为我没有找到一种方法来忽略所引用的内容.Pyparsing看起来是一个更好的答案,但我还没有找到一种方法来声明所有的语法.
目前,我正在使用我的旧Perl脚本来解析它,但我希望这是用Python编写的.如果你需要我的Perl片段,我很乐意提供它.
任何帮助表示赞赏.
利用现有解析器可能比使用ad-hoc正则表达式更好.
parse_http_list(s)
Parse lists as described by RFC 2068 Section 2.
In particular, parse comma-separated lists where the elements of
the list may include quoted-strings. A quoted-string could
contain a comma. A non-quoted string could have quotes in the
middle. Neither commas nor quotes count if they are escaped.
Only double-quotes count, not single-quotes.
parse_keqv_list(l)
Parse list of key=value strings where keys are not duplicated.
例:
>>> pprint.pprint(urllib2.parse_keqv_list(urllib2.parse_http_list(s)))
{'<118>date': '2010-05-09',
'classifier': 'Not,Spam',
'client_name': 'example.org,[194.177.17.24]',
'device_id': 'FE-2KA3F09000049',
'direction': 'in',
'disposition': 'Accept',
'from': 'prvs=4745cd07e1=example@example.org',
'log_id': '0400147717',
'log_part': '00',
'mailer': 'mta',
'message_length': '6832079',
'pri': 'information',
'resolved': 'OK',
'session_id': 'o49CedRc021772',
'subject':'=?windows-1255?B?Rlc6IEZ3OiDg5fDp5fog+fno5fog7Pf46eHp7S3u4+Tp7SE=?=',
'subtype': 'n/a',
'time': '16:41:27',
'to': 'example@example.org',
'type': 'statistics',
'virus': ''}
Run Code Online (Sandbox Code Playgroud)
我不确定你真正想要的是什么,但是
import re
data = "date=2010-05-09,time=16:41:27,device_id=FE-2KA3F09000049,log_id=0400147717,log_part=00,type=statistics,subtype=n/a,pri=information,session_id=o49CedRc021772,from=\"prvs=4745cd07e1=example@example.org\",mailer=\"mta\",client_name=\"example.org,[194.177.17.24]\",resolved=OK,to=\"example@example.org\",direction=\"in\",message_length=6832079,virus=\"\",disposition=\"Accept\",classifier=\"Not,Spam\",subject=\"=?windows-1255?B?Rlc6IEZ3OiDg5fDp5fog+fno5fog7Pf46eHp7S3u4+Tp7SE=?=\""
pattern = r"""(\w+)=((?:"(?:\\.|[^\\"])*"|'(?:\\.|[^\\'])*'|[^\\,"'])+)"""
print(re.findall(pattern, data))
Run Code Online (Sandbox Code Playgroud)
给你
[('date', '2010-05-09'), ('time', '16:41:27'), ('device_id', 'FE-2KA3F09000049'),
('log_id', '0400147717'), ('log_part', '00'), ('type', 'statistics'),
('subtype', 'n/a'), ('pri', 'information'), ('session_id', 'o49CedRc021772'),
('from', '"prvs=4745cd07e1=example@example.org"'), ('mailer', '"mta"'),
('client_name', '"example.org,[194.177.17.24]"'), ('resolved', 'OK'),
('to', '"example@example.org"'), ('direction', '"in"'),
('message_length', '6832079'), ('virus', '""'), ('disposition', '"Accept"'),
('classifier', '"Not,Spam"'),
('subject', '"=?windows-1255?B?Rlc6IEZ3OiDg5fDp5fog+fno5fog7Pf46eHp7S3u4+Tp7SE=?="')
]
Run Code Online (Sandbox Code Playgroud)
您可能希望之后清理引用的字符串(使用mystring.strip("'\"")).
编辑:这个正则表达式现在也正确处理引用的字符串(a="She said \"Hi!\"")内的转义引号.
正则表达式的解释:
(\w+)=((?:"(?:\\.|[^\\"])*"|'(?:\\.|[^\\'])*'|[^\\,"'])+)
Run Code Online (Sandbox Code Playgroud)
(\w+):匹配标识符并将其捕获到反向引用号.1
=:匹配一个 =
(:将以下内容捕获到反向引用号中.2:
(?::以下之一:
"(?:\\.|[^\\"])*":双引号,后跟零或多个以下内容:转义字符或非引用/非反斜杠字符,后跟另一个双引号
|: 要么
'(?:\\.|[^\\'])*':见上文,仅供单引号使用.
|: 要么
[^\\,"']:一个既不是反斜杠,也不是逗号,也不是引号的字符.
)+:重复至少一次,尽可能多次.
):捕获组的结束没有.2.