如何将字符串分解为字典

时间:2018-02-27 16:50:38

标签: python python-3.x dictionary

我有一个看起来像这样的字符串,

b'2018-02-27 11:42:40:b\'{"EventID":"65605751","Priority":"Lav Emerg","PriorityColor":"16725041","SortLevel":"7","VStationID":"1002","accepted_flag":"0","ack":"0","bedid":"42","elapseTimeInSeconds":"9","eventTimedOut":"0","failedname":" ","iconindex":"7","location":"1021","operating_mode":"0","pfunction_id":"8","priority_hw_id":"7","priorityindex":"2"}\''

我想将这些数据加载到一个看起来像这样的字典中,

{"EventID":"65605751","Priority":"Lav Emerg","PriorityColor":"16725041","SortLevel":"7","VStationID":"1002","accepted_flag":"0","ack":"0","bedid":"42","elapseTimeInSeconds":"9","eventTimedOut":"0","failedname":" ","iconindex":"7","location":"1021","operating_mode":"0","pfunction_id":"8","priority_hw_id":"7","priorityindex":"2"}

有人可以告诉我如何在Win7 Python 3.4上执行此操作吗?请注意,字符串长度可以变化。但是,日期,时间,{在开头和结尾都将确实存在。

3 个答案:

答案 0 :(得分:4)

您可以使用ast.literal_eval。此方法假定字典遵循{的第一次出现之后的所有内容。

import ast

mystr = """2018-02-27 11:42:40:b'{"EventID":"65605751","Priority":"Lav Emerg","PriorityColor":"16725041","SortLevel":"7","VStationID":"1002","accepted_flag":"0","ack":"0","bedid":"42","elapseTimeInSeconds":"9","eventTimedOut":"0","failedname":" ","iconindex":"7","location":"1021","operating_mode":"0","pfunction_id":"8","priority_hw_id":"7","priorityindex":"2"}"""

ast.literal_eval(mystr[mystr.index('{'):])

# {'EventID': '65605751',
#  'Priority': 'Lav Emerg',
#  'PriorityColor': '16725041',
#  'SortLevel': '7',
#  'VStationID': '1002',
#  'accepted_flag': '0',
#  'ack': '0',
#  'bedid': '42',
#  'elapseTimeInSeconds': '9',
#  'eventTimedOut': '0',
#  'failedname': ' ',
#  'iconindex': '7',
#  'location': '1021',
#  'operating_mode': '0',
#  'pfunction_id': '8',
#  'priority_hw_id': '7',
#  'priorityindex': '2'}

第二个字符串:

mystr = """b'2018-02-27 11:42:40:b\'{"EventID":"65605751","Priority":"Lav Emerg","PriorityColor":"16725041","SortLevel":"7","VStationID":"1002","accepted_flag":"0","ack":"0","bedid":"42","elapseTimeInSeconds":"9","eventTimedOut":"0","failedname":" ","iconindex":"7","location":"1021","operating_mode":"0","pfunction_id":"8","priority_hw_id":"7","priorityindex":"2"}\''"""

ast.literal_eval(mystr[mystr.index('{'):mystr.index('}')+1])

答案 1 :(得分:2)

假设前导日期,时间和:b'以及尾随'始终存在于字符串中且大小不变,那么您可以slice提取字符串数据。然后,假设数据是JSON,您可以使用json module将数据解码为字典。

import json

s = '2018-02-27 11:42:40:b\'{"EventID":"65605751","Priority":"Lav Emerg","PriorityColor":"16725041","SortLevel":"7","VStationID":"1002","accepted_flag":"0","ack":"0","bedid":"42","elapseTimeInSeconds":"9","eventTimedOut":"0","failedname":" ","iconindex":"7","location":"1021","operating_mode":"0","pfunction_id":"8","priority_hw_id":"7","priorityindex":"2"}\''

print(json.loads(s[22:-1]))

答案 2 :(得分:1)

from pprint import pprint
str = """2018-02-27 11:42:40:b'{"EventID":"65605751","Priority":"Lav Emerg","PriorityColor":"16725041","SortLevel":"7","VStationID":"1002","accepted_flag":"0","ack":"0","bedid":"42","elapseTimeInSeconds":"9","eventTimedOut":"0","failedname":" ","iconindex":"7","location":"1021","operating_mode":"0","pfunction_id":"8","priority_hw_id":"7","priorityindex":"2"}'"""

mydict = json.loads(str[str.find('{'):1+str.find('}')])
mydict = {unicode(k).encode("utf-8"): unicode(v).encode("utf-8") for k,v in mydict.iteritems()}
pprint(mydict)

<强>输出

{'EventID': '65605751',
 'Priority': 'Lav Emerg',
 'PriorityColor': '16725041',
 'SortLevel': '7',
 'VStationID': '1002',
 'accepted_flag': '0',
 'ack': '0',
 'bedid': '42',
 'elapseTimeInSeconds': '9',
 'eventTimedOut': '0',
 'failedname': ' ',
 'iconindex': '7',
 'location': '1021',
 'operating_mode': '0',
 'pfunction_id': '8',
 'priority_hw_id': '7',
 'priorityindex': '2'}