Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python列表中的隔离字符串_Python_String_List_Google Finance - Fatal编程技术网

python列表中的隔离字符串

python列表中的隔离字符串,python,string,list,google-finance,Python,String,List,Google Finance,我正在使用GoogleFinance模块获取Python中的股票数据。我想从列表中分离LastTradePrice并将其转换为整数。有什么建议吗?谢谢 from googlefinance import getQuotes import json stockinfo = getQuotes('VSMGX') stockdata = json.dumps(stockinfo, indent=1) 股票数据如下所示 [ { "ID": "904726512134516", "Last

我正在使用GoogleFinance模块获取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"
 }
]

实际上,你想要一个浮动,因为最后的交易价格有小数。迭代列表并提取字典中最后交易价格键的值

lastTradedPrices = []
for data in stockdata:
    lastTradedPrices.append(float(data["LastTradePrice"]))

如果所有记录都有最后交易价格,只需使用列表:

tradePrices = [ float(d["LastTradePrice"]) for d in stockdata ]

编辑:将其转换为@mattsap建议的
float
。好样的

你真的想要它是一个整数吗
22.26
将返回为
22
。stockdata中d的浮动(d['LastTradePrice'])有问题吗。顺便说一句
info1
不存在-你的意思是
stockinfo
?是的,我应该使用一个浮动。非常感谢。你的答案行得通,我刚刚写了与@AChampion相同的代码,代币换代币!嗯,这是一个成语:-)