Reputation: 350
The reason I say unknown is because it is being sent from an IOS 12 extension, I have no-hand in creating this format but lets just suppose there is no way I can do any formatting over there.
In Python what I receive is a string in the following format:
{
"body": '{\n carrierName = "AT&T";\n dateRecieved = "2018-08-07 20:29:56 +0000";\n \n}'
}
I want to know what this format {foo="bar"; top="long";} is called? It is what gets returned from iOS native Libraries so it has to be something; And is there any built-in way to parse it as a json or a dict. I can write my own parser for sure, but im looking for something built-in or something that exist in some generic library.
Upvotes: 2
Views: 313
Reputation: 5658
parsing without the py_mini_racer. I could not install py_mini_racer on my
python 3.7
d = {
"body": '{\n carrierName = "AT&T";\n dateRecieved = "2018-08-07 20:29:56 +0000";\n \n}'
}
def parseD(d):
import re
def makeD(l): # make dict from 2 element list
return dict([[s.strip().replace('"','') for s in l]])
finalD = {}
for di in [makeD(x) for x in [s.split('=') for s in re.findall(r'(\w+\s+=.*);', list(d.values())[0])]]:
finalD.update(di)
return finalD
parseD(d)
{'carrierName': 'AT&T', 'dateRecieved': '2018-08-07 20:29:56 +0000'}
d = parseD(d)
d['carrierName']
'AT&T'
d['dateRecieved']
'2018-08-07 20:29:56 +0000'
Upvotes: 2
Reputation: 57033
What you have in the dictionary looks like JavaScript. Fortunately, Python can interpret JS via py_mini_racer
. Install the module (say, with pip
), create an instance of the interpreter:
from py_mini_racer import py_mini_racer
js = py_mini_racer.MiniRacer()
d = {
"body": '{\n carrierName = "AT&T";\n dateRecieved = "2018-08-07 20:29:56 +0000";\n \n}'
}
Evaluate the expression and the variables, as needed:
js.eval(d['body'])
js.eval("carrierName")
#'AT&T'
js.eval("dateRecieved")
#'2018-08-07 20:29:56 +0000'
Upvotes: 3