Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将字符串分解成字典_Python_Python 3.x_Dictionary - Fatal编程技术网

Python 如何将字符串分解成字典

Python 如何将字符串分解成字典,python,python-3.x,dictionary,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":" ","

我有一根像这样的线

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上实现这一点吗?请注意,字符串长度可以是可变的。但是日期,时间,'{开头和结尾都肯定会在那里。

假设前导日期、时间和
:b'
,以及尾随
'
始终存在于字符串中,并且大小恒定,则可以使用字符串提取数据。然后,假设数据是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]))

您可以使用
ast.literal\u 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])
输出

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

我的字符串看起来与我在文章中提到的稍有不同。我更新了文章。你能告诉我你的代码中由于这个原因需要更改什么吗?你可以使用来识别
{…}
序列和json库,以便在dict中对其进行解析。
{'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'}