简体   繁体   English

Python将*未知格式的字符串解析为json或dict

[英]Python parsing *unknown format string to json or dict

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. 我说不明的原因是因为它是从IOS 12扩展发送的,我没有办法创建这种格式,但仅假设没有办法在那做任何格式化。

In Python what I receive is a string in the following format: 在Python中,我收到的是以下格式的字符串:

 {
   "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"; 我想知道这种格式{foo =“ bar”; top="long";} is called? top =“ long”;}被称为? It is what gets returned from iOS native Libraries so it has to be something; 它是从iOS本地库返回的内容,因此必须是某种东西。 And is there any built-in way to parse it as a json or a dict. 并且有任何内置的方式将其解析为json或dict。 I can write my own parser for sure, but im looking for something built-in or something that exist in some generic library. 我可以肯定地编写自己的解析器,但是我正在寻找内置的东西或某些通用库中存在的东西。

What you have in the dictionary looks like JavaScript. 字典中的内容类似于JavaScript。 Fortunately, Python can interpret JS via py_mini_racer . 幸运的是,Python可以通过py_mini_racer解释JS。 Install the module (say, with pip ), create an instance of the interpreter: 安装模块(例如,使用pip ),创建解释器的实例:

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'

parsing without the py_mini_racer. 没有py_mini_racer进行解析。 I could not install py_mini_racer on my 我无法在我的电脑上安装py_mini_racer
python 3.7 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM