2017-06-20 40 views
0

我想从googlefinance下载股票价格数据到excel使用Python 3.6。我使用下面的代码将数据导入Python。我现在有这些信息在数据中,但想将它保存在变量/数组中。但是,如果我尝试打印(数据['ID'])我得到“字符串索引必须是整数”?Python:googlefinance json.dumps error

from googlefinance import getQuotes 
import json 
print(json.dumps(getQuotes('AAPL'), indent=2)) 
[ 
    { 
    "Index": "NASDAQ", 
    "LastTradeWithCurrency": "129.09", 
    "LastTradeDateTime": "2015-03-02T16:04:29Z", 
    "LastTradePrice": "129.09", 
    "Yield": "1.46", 
    "LastTradeTime": "4:04PM EST", 
    "LastTradeDateTimeLong": "Mar 2, 4:04PM EST", 
    "Dividend": "0.47", 
    "StockSymbol": "AAPL", 
    "ID": "22144" 
    } 
] 
data = json.dumps(getQuotes('AAPL'), indent=2) 

回答

1

你有字典的名单,但你并不需要json.dumps,返回类型已经是一个列表。如果你想访问字典,你需要从列表中提取:

array_data = getQuotes('AAPL') 
data = array_data[0] 
print(data['ID']) 
+0

非常感谢!不幸的是我仍然得到错误消息:TypeError:字符串索引必须是打印后的整数(数据['ID']) – Rolf12

+0

嗨,我在本地测试。问题是json.dumps。你不需要那样做。我编辑了答案。希望这可以帮助 –

+0

非常感谢您的帮助! – Rolf12