Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break a string into a dictionary

I have a string that looks like this,

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"}\''

I would like to load this data into a dictionary that looks like this,

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

Can someone please tell me how to do this on Win7 Python 3.4? Please note that string length can be variable. But Date, time, '{ both in the beginning and end is going to be there for sure.

like image 935
usustarr Avatar asked Mar 07 '23 19:03

usustarr


2 Answers

You can use ast.literal_eval. This method assumes that the dictionary follows everything after and including the first occurrence of {.

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

For your second string:

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])
like image 68
jpp Avatar answered Mar 15 '23 06:03

jpp


Assuming that the leading date, time, and :b', and the trailing ' are always present in the string and are of constant size, then you could slice the string to extract the data. Then, assuming that the data is JSON, you can use the json module to decode the data into a dictionary.

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]))
like image 23
James Avatar answered Mar 15 '23 06:03

James