Python JSON 获取数据和检索列名的列表
Python JSON list to get data and retrieve column name
我是 Python 初学者。我正在访问一个历史金属价格 api,return 是一个列表字典。列表中的第一项是列名,其余是属于这些列的数据 headers.
我正在尝试编写代码来 return 记录所有日期的日期和现金买家值。
这是我到目前为止所写的内容。我已经尝试了几种字典和列表方法,但无法使它起作用。
URL 被故意限制为 1 行 (rows=1) 用于此发布并删除了我的 API 键。但是,此 URL 对任何人都有效,但在没有 API 密钥的情况下会受到速率限制:
import urllib
import json
url = "https://www.quandl.com/api/v1/datasets/LME/PR_CU.json?rows=1"
response = urllib.urlopen(url)
data = json.loads(response.read())
这是一个示例输出:
{
"code": "PR_CU",
"column_names": [
"Date",
"Cash Buyer",
"Cash Seller & Settlement",
"3-months Buyer",
"3-months Seller",
"15-months Buyer",
"15-months Seller",
"Dec 1 Buyer",
"Dec 1 Seller",
"Dec 2 Buyer",
"Dec 2 Seller",
"Dec 3 Buyer",
"Dec 3 Seller"
],
"data": [
[
"2016-10-14",
4672.0,
4672.5,
4691.0,
4692.0,
4730.0,
4740.0,
null,
null,
null,
null,
null,
null
]
],
"description": "LME Official Prices i
"display_url": null,
"errors": {},
"frequency": "daily",
"from_date": "2012-01-03",
"id": 19701916,
"name": "Copper Prices",
"premium": false,
"private": false,
"source_code": "LME",
"source_name": "London Metal Exchange"
"to_date": "2016-10-14",
"type": null,
"updated_at": "2016-10-17T07:05:00.54
"urlize_name": "Copper-Prices"
}
一种方法是获取 'Date' 和 'Cache Buyer' 列的位置,然后遍历 data 部分 JSON.
在我的示例中,我将从 JSON 加载的字典命名为字典:
# Get the columns and the data part of the response dictionary
columns = dictionary['column_names']
data = dictionary['data']
# This is done in case that the columns are not always in the same order
# if they are, you can just hardcode the values to 0 and 1
index_of_date = columns.index('Date')
index_of_cash_buyer = columns.index('Cash Buyer')
# As data section is a list of lists we need to
# iterate through lists of data and collect the desired values
for piece_of_data in data:
date = piece_of_data[index_of_date]
cash_buyer = piece_of_data[index_of_cash_buyer]
print date, cash_buyer
我是 Python 初学者。我正在访问一个历史金属价格 api,return 是一个列表字典。列表中的第一项是列名,其余是属于这些列的数据 headers.
我正在尝试编写代码来 return 记录所有日期的日期和现金买家值。
这是我到目前为止所写的内容。我已经尝试了几种字典和列表方法,但无法使它起作用。
URL 被故意限制为 1 行 (rows=1) 用于此发布并删除了我的 API 键。但是,此 URL 对任何人都有效,但在没有 API 密钥的情况下会受到速率限制:
import urllib
import json
url = "https://www.quandl.com/api/v1/datasets/LME/PR_CU.json?rows=1"
response = urllib.urlopen(url)
data = json.loads(response.read())
这是一个示例输出:
{
"code": "PR_CU",
"column_names": [
"Date",
"Cash Buyer",
"Cash Seller & Settlement",
"3-months Buyer",
"3-months Seller",
"15-months Buyer",
"15-months Seller",
"Dec 1 Buyer",
"Dec 1 Seller",
"Dec 2 Buyer",
"Dec 2 Seller",
"Dec 3 Buyer",
"Dec 3 Seller"
],
"data": [
[
"2016-10-14",
4672.0,
4672.5,
4691.0,
4692.0,
4730.0,
4740.0,
null,
null,
null,
null,
null,
null
]
],
"description": "LME Official Prices i
"display_url": null,
"errors": {},
"frequency": "daily",
"from_date": "2012-01-03",
"id": 19701916,
"name": "Copper Prices",
"premium": false,
"private": false,
"source_code": "LME",
"source_name": "London Metal Exchange"
"to_date": "2016-10-14",
"type": null,
"updated_at": "2016-10-17T07:05:00.54
"urlize_name": "Copper-Prices"
}
一种方法是获取 'Date' 和 'Cache Buyer' 列的位置,然后遍历 data 部分 JSON.
在我的示例中,我将从 JSON 加载的字典命名为字典:
# Get the columns and the data part of the response dictionary
columns = dictionary['column_names']
data = dictionary['data']
# This is done in case that the columns are not always in the same order
# if they are, you can just hardcode the values to 0 and 1
index_of_date = columns.index('Date')
index_of_cash_buyer = columns.index('Cash Buyer')
# As data section is a list of lists we need to
# iterate through lists of data and collect the desired values
for piece_of_data in data:
date = piece_of_data[index_of_date]
cash_buyer = piece_of_data[index_of_cash_buyer]
print date, cash_buyer