usustarr
usustarr

Reputation: 418

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.

Upvotes: 4

Views: 93

Answers (3)

jpp
jpp

Reputation: 164623

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

Upvotes: 4

murphy1310
murphy1310

Reputation: 667

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

Upvotes: 1

James
James

Reputation: 46

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

Upvotes: 2

Related Questions