我正在编写一个预测股票价格的程序,我需要搜索一个我创建的字典,里面填有公司名称和公司股票代码名称,这是我需要返回的,以便使用Quandl检索股票价格
下面是我如何创建字典的:
cnames= pd.read_csv('secwiki_tickers.csv')
cnamesDict= pd.Series(cnames.Ticker.values, index=cnames.Name).to_dict()#Fills a dictionary with the
csv file keys are company names values are ticker names
下面是我如何搜索字典并得到TypeError: argument of type 'float' is not iterable
错误:
user_cname = input("Which company would you like to predict stock prices for?\n")
def searchForName(dictToSearch, lookup):
for k,v in dictToSearch.items():
if user_cname in k: // here is where the error flags
return v
print(searchForName(cnamesDict, user_cname))
感谢您的帮助。CSV文件链接LINK
您应该使用
.get
方法直接在字典中查找公司名称,即相关问题 更多 >
编程相关推荐