我们有这样的字符串:
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...}
答案 0 :(得分:2)
答案 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'}
步骤:
userInfo:
;