打印特定记录 - 错误消息说String索引必须是整数

时间:2016-08-08 02:35:28

标签: python

我正在努力解决我的python代码问题 我想打印特定的column_names(例如日期打开等...)。当我尝试打印日期记录时,出现错误String indices must be integers。下面是我的代码和json文件的副本 我错过了什么?

import json, urllib
import quandl
url = "https://www.quandl.com/api/v3/datasets/WIKI/FB.json?"
loaded = urllib.urlopen(url).read()
data = json.loads(loaded)
for Date in data['dataset']['dataset_code']['Date']:
    print(Date)

JSON文件

{
    "dataset": {
        "dataset_code": "FB", 
        "column_names": [
            "Date", 
            "Open", 
            "High", 
            "Low", 
            "Close", 
            "Volume", 
            "Ex-Dividend", 
            "Split Ratio", 
            "Adj. Open", 
            "Adj. High", 
            "Adj. Low", 
            "Adj. Close", 
            "Adj. Volume"
        ],

1 个答案:

答案 0 :(得分:1)

"dataset_code"是一个字符串。

执行data['dataset']['dataset_code']后,结果为"FB""FB"是一个字符串,因此不能被另一个字符串索引。在您的示例文件中,data['dataset']['dataset_code']['Date']等同于"FB"['Date']。如果您在内部放置一个数字(“[]”),则只能在"FB"等字符串上使用integer运算符。

要对此数据执行任何有意义的操作,您可能需要将项重组为dict。这将使数据更容易使用。您可以使用此代码获取dicts列表:

cnames = data["dataset"]["column_names"]
entries = data["dataset"]["data"]
data_dict = [{c: entry[i] for i, c in enumerate(cnames)} for entry in entries]

现在,您的数据采用

的形式
[
  {
    "Volume": 20184035.0, 
    "Ex-Dividend": 0.0, 
    "Adj. High": 125.835, 
    "Adj. Close": 125.15, 
    "Adj. Volume": 20184035.0, 
    "High": 125.835, 
    "Adj. Low": 124.6184, 
    "Adj. Open": 124.98, 
    "Low": 124.6184, 
    "Date": "2016-08-05", 
    "Close": 125.15, 
    "Split Ratio": 1.0, 
    "Open": 124.98
  }, 
  {
    "Volume": 21065974.0, 
    "Ex-Dividend": 0.0, 
    "Adj. High": 124.79, 
    "Adj. Close": 124.36, 
    "Adj. Volume": 21065974.0, 
    "High": 124.79, 
    "Adj. Low": 122.51, 
    "Adj. Open": 122.94, 
    "Low": 122.51, 
    "Date": "2016-08-04", 
    "Close": 124.36, 
    "Split Ratio": 1.0, 
    "Open": 122.94
  },
]

使用这种新格式,选择单个行的所有值非常简单。要从数据集中获取所有日期,只需使用:

[x["Date"] for x in data2]

为方便起见,将其包装在一个函数中:

def get_all_values(key):
    return [x[key] for x in data2]

现在,您可以使用get_all_values("Date")get_all_values("Open")

您的完整代码将是:

import json, urllib
import quandl
url = "https://www.quandl.com/api/v3/datasets/WIKI/FB.json?"
loaded = urllib.urlopen(url).read()
data = json.loads(loaded)

cnames = data["dataset"]["column_names"]
entries = data["dataset"]["data"]
data2 = [{c: entry[i] for i, c in enumerate(cnames)} for entry in entries]


def get_all_values(key):
    return [x[key] for x in data2]

# Get all the dates from the dataset
for Date in get_all_values("Date"):
    print(Date)

希望我帮忙!