首页 > 解决方案 > python - 使用 yahoo_fin 生成的字典中的数据

问题描述

我是 python 新手,我的目标是编写一些用于股票分析的代码。我已经安装了yahoo_fin并开始尝试使用它。我已经开始使用get_live_price

提取实时报价, 即

>>> get_live_price('aapl')
126.12000274658203

我正在处理以美元为单位的股票以及以加元为单位的股票,这使我需要以共同货币表示它们。所以我想将所有美元股票转换为加元,反之亦然。当然,在我可以转换货币之前,我需要先确定一只股票上市的货币。我发现yahoo_fin.stock_info包括get_quote_data例如

>>> get_quote_data('aapl')

产生了这本词典

{'language': 'en-US', 'region': 'US', 'quoteType': 'EQUITY', 'quoteSourceName': 'Nasdaq Real Time Price', 'triggerable': True, 'currency': 'USD ', 'epsTrailingTwelveMonths': 3.687, 'epsForward': 4.68, 'epsCurrentYear': 4.45, 'priceEpsCurrentYear': 28.476248, 'sharesOutstanding': 16788100096, 'bookValue': 3.936, 'fiftyDayAverage': 133.55576, 'fiftyDayAverageChange': -6.8364563 ,“ fiftydayaveragechangepercent':-0.051188033,'twundredDayAaverager':122.48235,'twhundredDayaverageChange':4.236946,'twhundredDayaveragechange':0.034592297,'''':0.034592297,''''''';:15,'exchangeDataDelayedBy':0,'tradeable':False,'marketState':'REGULAR','regularMarketChange':-3.1506958,'regularMarketChangePercent':-2.4260383,'regularMarketTime':1614010862,'regularMarketPrice':126.7193,' regularMarketDayHigh':129.72,'regularMarketDayRange':'125.6 - 129.72','regularMarketDayLow':125.6,'regularMarketVolume':45892833,'regularMarketPreviousClose':129.87,'bid':126.36,'ask':126.3,'bidize':8Sbidize ,'askSize':8,'fullExchangeName':'NasdaqGS','financialCurrency':'USD','regularMarketOpen':128.01,'averageDailyVolume3Month':104097755,'averageDailyVolume10Day':84604500,'fiftyTwoWeekLowChange':73。5668, 'fiftyTwoWeekLowChangePercent': 1.3840704, 'fiftyTwoWeekRange': '53.1525 - 145.09', 'fiftyTwoWeekHighChange': -18.370697, 'fiftyTwoWeekHighChangePercent': -0.12661588, 'fiftyTwoWeekLow': 53.1525, 'fiftyTwoWeekHigh': 145.09, 'dividendDate': 1613001600, 'earningsTimestamp': 1611765000, 'earningsTimestampStart': 1619607540, 'earningsTimestampEnd': 1620043200, 'trailingAnnualDividendRate': 0.807, 'trailingPE': 34.369217, 'trailingAnnualDividendYield': 0.006213906, 'firstTradeDateMilliseconds': 345479400000, 'priceHint': 2, 'exchange ':'NMS','shortName':'Apple Inc.','longName':'Apple Inc.','messageBoardId':'finmb_24937','exchangeTimezoneName':'America/New_York', 'exchangeTimezoneShortName': 'EST', 'gmtOffSetMilliseconds': -18000000, 'market': 'us_market', 'esgPopulated': False, 'displayName': 'Apple', 'symbol': 'AAPL' }

我不熟悉字典,也不知道如何处理这些数据,但在那长长的数据列表的早期,我看到了

'货币:美元'

所以我的问题是,我怎样才能从这样的字典中只获取货币数据或任何其他数据。

标签: pythondictionarylibrariescurrencystock

解决方案


所以我的问题是,我怎样才能从这样的字典中只获取货币数据或任何其他数据。

存在键值对的字典。在您的具体情况下,您可以'currency':'USD'通过以下方式访问:

data = {'language': 'en-US', 'region': 'US', 'quoteType': 'EQUITY', 'quoteSourceName': 'Nasdaq Real Time Price', 'triggerable': True, 'currency': 'USD', 'epsTrailingTwelveMonths': 3.687, 'epsForward': 4.68, 'epsCurrentYear': 4.45, 'priceEpsCurrentYear': 28.476248, 'sharesOutstanding': 16788100096, 'bookValue': 3.936, 'fiftyDayAverage': 133.55576, 'fiftyDayAverageChange': -6.8364563, 'fiftyDayAverageChangePercent': -0.051188033, 'twoHundredDayAverage': 122.48235, 'twoHundredDayAverageChange': 4.236946, 'twoHundredDayAverageChangePercent': 0.034592297, 'marketCap': 2127376285696, 'forwardPE': 27.076775, 'priceToBook': 32.194942, 'sourceInterval': 15, 'exchangeDataDelayedBy': 0, 'tradeable': False, 'marketState': 'REGULAR', 'regularMarketChange': -3.1506958, 'regularMarketChangePercent': -2.4260383, 'regularMarketTime': 1614010862, 'regularMarketPrice': 126.7193, 'regularMarketDayHigh': 129.72, 'regularMarketDayRange': '125.6 - 129.72', 'regularMarketDayLow': 125.6, 'regularMarketVolume': 45892833, 'regularMarketPreviousClose': 129.87, 'bid': 126.36, 'ask': 126.3, 'bidSize': 8, 'askSize': 8, 'fullExchangeName': 'NasdaqGS', 'financialCurrency': 'USD', 'regularMarketOpen': 128.01, 'averageDailyVolume3Month': 104097755, 'averageDailyVolume10Day': 84604500, 'fiftyTwoWeekLowChange': 73.5668, 'fiftyTwoWeekLowChangePercent': 1.3840704, 'fiftyTwoWeekRange': '53.1525 - 145.09', 'fiftyTwoWeekHighChange': -18.370697, 'fiftyTwoWeekHighChangePercent': -0.12661588, 'fiftyTwoWeekLow': 53.1525, 'fiftyTwoWeekHigh': 145.09, 'dividendDate': 1613001600, 'earningsTimestamp': 1611765000, 'earningsTimestampStart': 1619607540, 'earningsTimestampEnd': 1620043200, 'trailingAnnualDividendRate': 0.807, 'trailingPE': 34.369217, 'trailingAnnualDividendYield': 0.006213906, 'firstTradeDateMilliseconds': 345479400000, 'priceHint': 2, 'exchange': 'NMS', 'shortName': 'Apple Inc.', 'longName': 'Apple Inc.', 'messageBoardId': 'finmb_24937', 'exchangeTimezoneName': 'America/New_York', 'exchangeTimezoneShortName': 'EST', 'gmtOffSetMilliseconds': -18000000, 'market': 'us_market', 'esgPopulated': False, 'displayName': 'Apple', 'symbol': 'AAPL'}
print(data['currency'])

输出将是USDcurrency从字典中访问键的值data


为了进一步阅读字典,我建议:


推荐阅读