Rolf12 Rolf12 - 9 months ago 36
Python Question

Python: googlefinance json.dumps error

I am trying to download stock price data from googlefinance to excel using Python 3.6. I use the following code to get the data into Python. I have this information now in data but would like to save it in variables/array. However if I try print (data['ID']) I get "string indices must be integers"?

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)

Answer Source

You have a list of a dict, but you don't need json.dumps, the return type is already a list. If you want to access the dictionary, you need to extract it from the list:

array_data = getQuotes('AAPL')
data = array_data[0]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download