如何将字符串拆分成字典

4
我有一个字符串看起来像这样,
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上完成这个任务吗?请注意字符串长度可能是可变的。但日期,时间,“{”和“}”在开头和结尾肯定会出现。


你可以使用正则表达式来识别{...}序列,并使用json库将其解析为字典。 - Laur Ivan
3个回答

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])

我的字符串看起来与我在帖子中提到的略有不同。我已经更新了帖子。您能告诉我由于这个需要在您的代码中更改什么吗? - usustarr

2

假设字符串中始终存在日期、时间和:b'前缀以及结尾的'后缀,并且它们的大小是固定的,那么您可以使用slice函数来提取数据。然后,假设数据是JSON格式的,您可以使用json模块将数据解码为字典。

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]))

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)

Output:

{'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'}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接