首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Python删除JSON的头/元数据

如何使用Python删除JSON的头/元数据
EN

Stack Overflow用户
提问于 2016-03-06 17:34:39
回答 1查看 2.5K关注 0票数 1

我在学习Python。我一直试图从Quandl中提取数据,我成功地加载了数据,但是当我尝试将其转换为Python时,它会抛出一个ValueError!

代码语言:javascript
运行
复制
    ValueError                                Traceback (most recent call last)

 <ipython-input-10-0b58998505ee> in <module>()
----> 1 data=dict(data)

ValueError: dictionary update sequence element #0 has length 1; 2 is required

下面是我的代码,

代码语言:javascript
运行
复制
import json,urllib2
url = "https://www.quandl.com/api/v3/datasets/NASDAQOMX/NQASIA0001LMGBPT.json"
loaded = urllib2.urlopen(url).read()
data = json.loads(loaded)
type(data) # shows string
data=dict(data) # here i'm getting value error

数据就是这样的

代码语言:javascript
运行
复制
  {"dataset":{"id":12835494,"dataset_code":"NQASIA0001LMGBPT","database_code":"NASDAQOMX","name":"NASDAQ Asia Oil \u0026 Gas Large Mid Cap GBP TR Index (NQASIA0001LMGBPT)","description":"  \u003cp\u003eThe comprehensive NASDAQ Global Index Family covers international securities segmented by geography, sector, and size. NASDAQ OMX's transparent and rules-based selection method results in a complete representation of the global investable equity marketplace. The indexes cover 45 individual countries within Developed and Emerging Markets, and facilitate a multitude of tracking, trading, and investing opportunities.\u003c/p\u003e\n  \u003cp\u003e\u003cb\u003eComponents:\u003c/b\u003e \u003ca href=https://indexes.nasdaqomx.com/Index/Weighting/NQASIA0001LMGBPT\u003e40\u003c/a\u003e\u003c/p\u003e\n  \u003cp\u003e\u003cb\u003eCurrency:\u003c/b\u003e GBP\n  \u003cp\u003e\u003cb\u003eEntitlements:\u003c/b\u003e \u003ca href=https://indexes.nasdaqomx.com/Index/Overview/NQASIA0001LMGBP\u003eNASDAQ Global Index Family\u003c/a\u003e\u003c/p\u003e\n  \u003cp\u003e\u003cb\u003eTotal Market Value:\u003c/b\u003e is the sum of the market value of all companies in the index.\u003c/p\u003e\n  \u003cp\u003e\u003cb\u003eDividend Market Value:\u003c/b\u003e is the sum of the market value of all dividends generated by companies in the index. \u003c/p\u003e\n  \u003cp\u003e\u003cb\u003eTerms of Use:\u003c/b\u003e This data cannot be republished or used as the basis of a financial product without the permission of Nasdaq OMX.\u003c/p\u003e\n\n","refreshed_at":"2016-03-05T02:26:29.308Z","newest_available_date":"2016-03-04","oldest_available_date":"2001-03-30","column_names":["Trade Date","Index Value","High","Low","Total Market Value","Dividend Market Value"],"frequency":"daily","type":"Time Series","premium":false,"limit":null,"transform":null,"column_index":null,"start_date":"2001-03-30","end_date":"2016-03-04","data":[["2016-03-04",901.68,901.68,901.68,120990409547.0,10184040.0],["2016-03-03",888.22,888.22,888.22,119195278884.0,74919059.0],["2016-03-02",876.66,876.66,876.66,117717482960.0,0.0],["2016-03-01",861.69,861.69,861.69,115706487736.0,31420802.0],["2016-02-29",840.1,840.1,840.1,112838933060.0,0.0],["2016-02-26",856.96,856.96,856.96,115103827172.0,0.0],["2016-02-25",836.8,836.8,836.8,112395722181.0,43584397.0],["2016-02-24",846.48,846.48,846.48,113739936161.0,29138803.0],["2016-02-23",846.58,846.58,846.58,113782545450.0,0.0],["2016-02-22",839.75,839.75,839.75,112864607315.0,0.0],["2016-02-19",833.0,833.0,833.0,111957089747.0,0.0],["2016-02-18",832.63,832.63,832.63,111907945844.0,0.0],["2016-02-17",808.34,808.34,808.34,108642319107.0,0.0],["2016-02-16",821.18,821.18,821.18,110368331892.0,7742456.0],["2016-02-15",801.89,801.89,801.89,107783839163.0,0.0],["2016-02-12",770.54,770.54,770.54,103569144401.0,0.0],["2016-02-

我想我在这里遗漏了一些东西,我需要对URL做些什么,传递参数吗?或者在data=dict(数据)之前我还需要包括什么步骤呢?

在发布这个问题之前,我检查过StackOverFlow,我没有成功,我做了google,我点击的每个站点都使用Quandl包,(下面链接)#Quandl(数据集)

任何描述/教程都会让我更好地理解这一点。耽误您时间,实在对不起。

PS:我想在不使用熊猫和其他图书馆的情况下这样做。

谢谢,

Retrieving data from Quandl with Python

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-06 17:42:15

使用requests

代码语言:javascript
运行
复制
import json, requests
url = "https://www.quandl.com/api/v3/datasets/NASDAQOMX/NQASIA0001LMGBPT.json"
data = requests.get(url).json()
print(data)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35830100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档