解析JSON msg时遇到问题

时间:2018-12-23 17:38:52

标签: python-3.x sqlite quandl

我正在调用QUANDL API来获取数据,并返回一个JSON msg,这在解析到发送到数据库之前遇到了麻烦。我的解析代码显然无法正确读取JSON。

通过以下代码,我得到以下(为简单起见被截断)JSON: {“ datatable”:{“ data”:[[“ AAPL”,“ MRY”,“ 2018-09-29”,265595000000],[“ AAPL”,“ MRY”,“ 2017-09-30”,229234000000] ,[“ AAPL”,“ MRY”,“ 2016-09-24”,215639000000],[“ AAPL”,“ MRY”,“ 2015-09-26”,233715000000],[“ AAPL”,“ MRY”, “ 2014-09-27”,182795000000],[“ AAPL”,“ MRY”,“ 2013-09-28”,170910000000],[“ AAPL”,“ MRT”,“ 2018-09-29”,265595000000] ,[“ AAPL”,“ MRT”,“ 2018-06-30”,255274000000],[“ AAPL”,“ MRT”,“ 2018-03-31”,247417000000],[“ AAPL”,“ MRT”, “ 2017-12-30”,239176000000],[“ AAPL”,“ MRT”,“ 2017-09-30”,229234000000],[“ AAPL”,“ MRT”,“ 2017-07-01”,223507000000] ,[“ AAPL”,“ MRT”,“ 2017-04-01”,220457000000],[“ AAPL”,“ MRT”,“ 2016-12-31”,218118000000],[“ AAPL”,“ MRT”, “ 2016-09-24”,215639000000],[“ AAPL”,“ MRT”,“ 2016-06-25”,220288000000],[“ AAPL”,“ MRT”,“ 2016-03-26”,227535000000] ,[“ AAPL”,“ MRT”,“ 2015-12-26”,234988000000],[“ AAPL”,“ MRT”,“ 2015-09-26”,233715000000],[“ AAPL”,“ MRT”, “ 2015-06-27”,224337000000],[“ AAPL”,“ MRT”,“ 2015-03-28”,212164000000],[“ AAPL”,“ MRT”,“ 2014-12-27”,199800000000] ,[“ AAPL”,“ MRT”,“ 2014-09-27”,182795000000],[“ AAPL”,“ MRT”,“ 2014-06-28”,17814400 0000],[“ AAPL”,“ MRT”,“ 2014-03-29”,176035000000],“列”:[{“ name”:“ ticker”,“ type”:“ String”},{“ name” :“维度”,“类型”:“字符串”},{“名称”:“日期键”,“类型”:“日期”},{“名称”:“收入”,“类型”:“整数”}] },“ meta”:{“ next_cursor_id”:null}}

import quandl, requests
from flask import request
from cs50 import SQL
db = SQL("sqlite:///formula.db")

data = 
requests.get(f"https://www.quandl.com/api/v3/datatables/SHARADAR/SF1.json?ticker=AAPL&qopts.columns=ticker,dimension,datekey,revenue&api_key=YOURAPIKEY")

responses = data.json()
print(responses)

for response in responses:
    ticker=str(response["ticker"])
    dimension=str(response["dimension"])
    datekey=str(response["datekey"])
    revenue=int(response["revenue"])
    db.execute("INSERT INTO new(ticker, dimension, datekey, revenue) VALUES(:ticker, :dimension, :keydate, :revenue)", ticker=ticker, dimension=dimension, datekey=datekey, revenue=revenue)

我收到以下错误消息(我过去曾遇到过,并已成功解决),因此坚信我没有正确阅读json:     在第12行的文件“ new2.py”中         ticker = str(response [“ ticker”])     TypeError:字符串索引必须为整数

我希望能够遍历json并能够隔离特定数据,然后填充数据库。

1 个答案:

答案 0 :(得分:0)

对于您的响应结构,您有一个嵌套的dict对象:

datatable
   data
      list of lists of data

因此,这将发生:

responses = data.json()
datatable = responses['datatable']  # will get you the information mapped to the 'datatables' key
datatable_data = datatable['data']  # will get you the list mapped to the 'data' key

现在,datatable_data是列表列表,对吧?并且列表只能通过索引点访问,不能通过字符串访问

所以,假设您要第一个响应。

first_response = datatable_data[0]

这将导致

first_response = ["AAPL","MRY","2018-09-29",265595000000]

您现在可以通过索引点对其进行访问:

for idx, val in enumerate(first_response):
    print(f'{idx}\t{val}')

将打印出来

0   AAPL
1   MRY
2   2018-09-29
3   265595000000

因此,利用所有这些信息,您需要更改程序以确保您访问响应中的data键,然后遍历列表列表。

所以,像这样:

data = responses['datatable']['data']
for record in data:
   ticker, dimension, datekey, revenue = record  # unpack list into named variables
   db.execute(...)
相关问题