我正在使用 Google Finance 模块来获取 Python 中的股票数据。我想从列表中分离出 LastTradePrice 并将其转换为整数。有什么建议么?谢谢。
from googlefinance import getQuotes
import json
stockinfo = getQuotes('VSMGX')
stockdata = json.dumps(stockinfo, indent=1)
股票数据如下所示
[
{
"ID": "904726512134516",
"LastTradeDateTimeLong": "Jan 8, 4:00PM EST",
"Index": "MUTF",
"LastTradeWithCurrency": "$22.26",
"LastTradeTime": "4:00PM EST",
"StockSymbol": "VSMGX",
"LastTradePrice": "22.26",
"LastTradeDateTime": "2016-01-08T16:00:00Z"
}
]
最佳答案
如果所有记录都有最后交易价格,则只需使用列表理解:
tradePrices = [ float(d["LastTradePrice"]) for d in stockdata ]
编辑:按照@mattsap的建议将其转换为float
。发现得很好!
关于python - 隔离Python列表中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34697461/