在Python中将字符串转换为嵌套字典

时间:2020-11-02 10:33:02

标签: python string dictionary

我正在使用API来返回格式化为嵌套字典的字符串。

问题在于返回的字符串结构与嵌套字典非常相似,但是并不完全相同。

下面是API实际返回的示例:

import pyOpt

def fun(x):
    o = (1-x[0])**2 + 1*(x[1] - x[0]**2)**2
    return o, [], 0

opt_prob = pyOpt.Optimization('rosenbrock_11', fun)
slsqp = pyOpt.SLSQP()
opt_prob.addObj('f')
opt_prob.addVar('x', 'c', value=0, lower=-10, upper=10)
opt_prob.addVar('y', 'c', value=2, lower=-10, upper=10)
slsqp(opt_prob)

res = opt_prob.solution(0)
vs = res.getVarSet()
x = [vs[key].value for key in vs]
print(x)
# [1.000128987486733, 1.0002557449445557]

RAW文字

{
    "quote":{
        "symbol":"AMZN"
        "companyName":"Amazon.com Inc."
        "primaryExchange":"Nasdaq Global Select"
        "sector":"Consumer Cyclical"
        "calculationPrice":"close"
        "open":3157.75
        "openTime":1604064600266
        "close":1739.65
        "closeTime":1604088001623
        "high":3167.004
        "low":3019
        "latestPrice":1739.65
        "latestSource":"Close"
        "latestTime":"October 30, 2020"
        "latestUpdate":1604088001623
        "latestVolume":NULL
        "iexRealtimePrice":NULL
        "iexRealtimeSize":NULL
        "iexLastUpdated":NULL
        "delayedPrice":3036.15
        "delayedPriceTime":1604088001623
        "extendedPrice":3044.2
        "extendedChange":1304.55
        "extendedChangePercent":0.74989
        "extendedPriceTime":1604091597028
        "previousClose":1724.42
        "change":NULL
        "changePercent":NULL
        "iexMarketPercent":NULL
        "iexVolume":NULL
        "avgTotalVolume":3150697
        "iexBidPrice":NULL
        "iexBidSize":NULL
        "iexAskPrice":NULL
        "iexAskSize":NULL
        "marketCap":860528336495
        "peRatio":NULL
        "week52High":3167.004
        "week52Low":1307
        "ytdChange":0.13911139273485668
        }
    "bids":[
        ]
    "asks":[
        ]
    "systemEvent":{
        }
}

如您所见,报价字典的格式设置为普通字典,问题在于字典{"quote":{"symbol":"AAPL","companyName":"Apple Inc.","primaryExchange":"Nasdaq Global Select","sector":"Technology","calculationPrice":"close","open":111,"openTime":1604064600921,"close":227.01,"closeTime":1604088000397,"high":111.99,"low":107.72,"latestPrice":227.01,"latestSource":"Close","latestTime":"October 30, 2020","latestUpdate":1604088000397,"latestVolume":null,"iexRealtimePrice":null,"iexRealtimeSize":null,"iexLastUpdated":null,"delayedPrice":108.86,"delayedPriceTime":1604088000397,"extendedPrice":109.05,"extendedChange":-117.96,"extendedChangePercent":-0.51962,"extendedPriceTime":1604091597895,"previousClose":220.82,"change":null,"changePercent":null,"iexMarketPercent":null,"iexVolume":null,"avgTotalVolume":28048177,"iexBidPrice":null,"iexBidSize":null,"iexAskPrice":null,"iexAskSize":null,"marketCap":1025899051800,"peRatio":null,"week52High":228.22,"week52Low":107.72,"ytdChange":0.48276164267212823},"bids":[],"asks":[],"systemEvent":{}} bids是空的,并且具有方括号。< / p>

我已经尝试过此方法Converting a string to a nested dictionary,但由于嵌套字典不是标准格式,因此它显然不起作用

最终的目标是从字典中提取值,因为目前只有一个字符串是不可能的。

0 个答案:

没有答案