如何用python提取信息入库

oWangShiRuGe 2012-07-24 09:51:47
Jul 24 18:03:11 192.168.0.13 [login] time:20120724 18:03:11 user:大坏蛋 ip:211.143.50.33 result:succeed channel:10086 ua:{"HTTP_USER_AGENT":"-","HTTP_X_FORWARDED_FOR":"211.143.50.33","clientip":"211.143.50.33","HTTP_X_REAL_IP":"-"} domain:service.xxx.com
Jul 24 18:03:11 192.168.0.13 [login] time:20120724 18:03:11 user:as1984 ip:218.205.245.21 result:succeed channel:10086 ua:{"HTTP_USER_AGENT":"-","HTTP_X_FORWARDED_FOR":"218.205.245.21","clientip":"218.205.245.21","HTTP_X_REAL_IP":"-"} domain:service.xxx.com


上面是2行信息

需要提取以下几个字段后面的信息入库:
time:
user:
ip:
result:
ua:
domain:

user会是中文
请教如何用python实现?
...全文
99 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
oWangShiRuGe 2012-07-25
  • 打赏
  • 举报
回复
原始信息中 每个字段的分隔符是\t
oWangShiRuGe 2012-07-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

Python code
import re

pat = re.compile(r'time:(.*?) user:(.*?) ip:(.*?) result:(.*?) ua:(.*?) domain:(.*)', re.I)
with open('test.txt') as fd:
for line in fd:
mat = pat.search(line)
……
[/Quote]

我想把[login]也匹配上 因为还可以出现不是[login]的情况 这样的我需要入到其它表
另外 结果中含有\t 能不能去掉啊
oWangShiRuGe 2012-07-25
  • 打赏
  • 举报
回复
解决了 谢谢
bugs2k 2012-07-25
  • 打赏
  • 举报
回复
pat = re.compile(r'\[login\]\s*time:(.*?)\s*user:(.*?)\s*ip:(.*?)\s*result:(.*?)\s*ua:(.*?)\s*domain:(.*)', re.I)
bugs2k 2012-07-24
  • 打赏
  • 举报
回复
import re

pat = re.compile(r'time:(.*?) user:(.*?) ip:(.*?) result:(.*?) ua:(.*?) domain:(.*)', re.I)
with open('test.txt') as fd:
for line in fd:
mat = pat.search(line)
if mat:
print ' | '.join(mat.groups())


test.txt 包含上面两行文本

37,742

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧