gpt4 book ai didi

python - 如何将 Quandl json 转换为 Python 字典?

转载 作者:太空宇宙 更新时间:2023-11-03 20:02:32 26 4
gpt4 key购买 nike

{
"dataset": {
"id": 2288715,
"dataset_code": "ORB",
"database_code": "OPEC",
"name": "OPEC Crude Oil Price",
"description": "Reference Price for the OPEC Crude Oil Basket. Currently includes: Saharan Blend (Algeria), Girassol (Angola), Oriente (Ecuador), Iran Heavy (Islamic Republic of Iran), Basra Light (Iraq), Kuwait Export (Kuwait), Es Sider (Libya), Bonny Light (Nigeria), Qatar Marine (Qatar), Arab Light (Saudi Arabia), Murban (UAE) and Merey (Venezuela).",
"refreshed_at": "2019-12-02T12:25:30.433Z",
"newest_available_date": "2019-11-29",
"oldest_available_date": "2003-01-02",
"column_names": [
"Date",
"Value"
],
"frequency": "daily",
"type": "Time Series",
"premium": false,
"limit": null,
"transform": null,
"column_index": null,
"start_date": "2019-11-26",
"end_date": "2019-11-26",
"data": [
[
"2019-11-26",
63.92
]
],
"collapse": null,
"order": null,
"database_id": 381
}
}

我尝试了下面的代码,但它不起作用,并且不在 HTML 上显示详细信息。如何将json转换为python以便数据显示在html页面上?

        #convert from JSON string to Python Dictionary
oilData = json.loads(response.text)
#TO retrieve the latest data from Meta Data
lastRefreshedDate = oilData["data"]
#To retrieve latest stock price
latestOilPrices = oilData["Date"][lastRefreshedDate]
closingPrice = latestOilPrices["Value"]
return render_template('oil_price.html',tCode=Code,sPrice=closingPrice,dTime=lastRefreshedDate)

编辑:遇到错误

Traceback (most recent call last):
File "C:\Program Files\Python38\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Program Files\Python38\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Program Files\Python38\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Program Files\Python38\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Program Files\Python38\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Program Files\Python38\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\shirl\Downloads\OBKD_Part2_Group (1)\oBKD part 2\app.py", line 138, in result
latestOilPrices = oilData["data"][lastRefreshedDate]
KeyError: 'data'
127.0.0.1 - - [03/Dec/2019 12:30:48] "POST /result HTTP/1.1" 500 -

最佳答案

这应该修复数据,以便 python 可以读取:

import re
jdata = re.sub(r'(?:false)','False',jdata)
jdata = re.sub(r'(?:null)','None',jdata)
pydict = eval(jdata)

关于python - 如何将 Quandl json 转换为 Python 字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59149986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com