python str转dict,将str转换为python中的dict

I got this from a process's output using subprocess.Popen() :

{ about: 'RRDtool xport JSON output',

meta: {

start: 1401778440,

step: 60,

end: 1401778440,

legend: [

'rta_MIN',

'rta_MAX',

'rta_AVERAGE'

]

},

data: [

[ null, null, null ],

[ null, null, null ],

[ null, null, null ],

[ null, null, null ],

[ null, null, null ],

[ null, null, null ]

]

}

It doesn't seem to be a valid json to me.

I have used ast.literal_eval() and json.loads(), but with no luck.

Can someone help me in the right direction ?

Thanks in advance.

解决方案

Indeed, older versions of rddtool export ECMA-script, not JSON. According to this debian bug report upgrading 1.4.8 should give you proper JSON. Also see the project CHANGELOG:

JSON output of xport is now actually json compilant by its keys

being properly quoted now.

If you cannot upgrade, you have two options here; either attempt to reformat to apply quoting the object key identifiers, or use a parser that's more lenient and parses ECMA-script object notation.

The latter can be done with the external demjson library:

>>> import demjson

>>> demjson.decode('''\

... { about: 'RRDtool xport JSON output',

... meta: {

... start: 1401778440,

... step: 60,

... end: 1401778440,

... legend: [

... 'rta_MIN',

... 'rta_MAX',

... 'rta_AVERAGE'

... ]

... },

... data: [

... [ null, null, null ],

... [ null, null, null ],

... [ null, null, null ],

... [ null, null, null ],

... [ null, null, null ],

... [ null, null, null ]

... ]

... }''')

{u'about': u'RRDtool xport JSON output', u'meta': {u'start': 1401778440, u'step': 60, u'end': 1401778440, u'legend': [u'rta_MIN', u'rta_MAX', u'rta_AVERAGE']}, u'data': [[None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None], [None, None, None]]}

Repairing can be done using a regular expression; I am going to assume that all identifiers are on a new line or directly after the opening { curly brace. Single quotes in the list will have to be changed to double quotes; this will only work if there are no embedded single quotes in the values too:

import re

import json

yourtext = re.sub(r'(?:^|(?<={))\s*(\w+)(?=:)', r' "\1"', yourtext, flags=re.M)

yourtext = re.sub(r"'", r'"', yourtext)

data = json.loads(yourtext)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值