如何使用python的re模块从以下字符串中获取键值样式信息

时间:2017-02-08 12:17:09

标签: python regex

我们有这样的字符串:

2016-11-10 00:00:44,766 INFO [com.server.log.Log] - <Login Success. userInfo:  userID = 140803;appID = 3;channelID = 309;channelUserID = 76ab5a49ea40477f9623074d0d5e0227;userName = 1477806062675.ios.test;channelUserName = l379192229;createTime = 2016-10-30 13:41:02.0;loginTime = 1478707244762;userIP = 61.158.146.157;IMEI = FB7FE841-9E54-4421-86E0-1ED625068C57;newUser = false>

现在我想通过使用python的re模块从字符串中提取键值信息。

因此,它应该是一个词典:{'userID': 140803, 'channelid':309...}

2 个答案:

答案 0 :(得分:2)

你走了:

n

请参阅a demo on ideone.com

答案 1 :(得分:0)

您也可以这样做:

import re

a = """2016-11-10 00:00:44,766 INFO [com.server.log.Log] - <Login Success. userInfo:  userID = 140803;appID = 3;channelID = 309;channelUserID = 76ab5a49ea40477f9623074d0d5e0227;userName = 1477806062675.ios.test;channelUserName = l379192229;createTime = 2016-10-30 13:41:02.0;loginTime = 1478707244762;userIP = 61.158.146.157;IMEI = FB7FE841-9E54-4421-86E0-1ED625068C57;newUser = false>"""
a = [re.split('\s*=\s*',b) for b in re.findall(r'(?:userInfo:\s*)(.*)', a)[0].split(';')]
d = dict(a)
d

输出:

{'IMEI': 'FB7FE841-9E54-4421-86E0-1ED625068C57',
 'appID': '3',
 'channelID': '309',
 'channelUserID': '76ab5a49ea40477f9623074d0d5e0227',
 'channelUserName': 'l379192229',
 'createTime': '2016-10-30 13:41:02.0',
 'loginTime': '1478707244762',
 'newUser': 'false>',
 'userID': '140803',
 'userIP': '61.158.146.157',
 'userName': '1477806062675.ios.test'}

步骤:

  1. 选择userInfo:
  2. 之后的所有内容
  3. 拆分;
  4. 在步骤2中循环显示列表并在\ s * = \ s *。
  5. 上拆分
  6. 从步骤3的列表列表中创建字典