Python KeyError:0,您能帮我找到错误吗?
我的python代码中有一个keyError 0 我真的不明白这意味着什么,在我的情况下,我读了很多关于它,但我不能找到我自己的错误 谁能帮我找到它,也许能给我解释一下 问候,Python KeyError:0,您能帮我找到错误吗?,python,dictionary,keyerror,Python,Dictionary,Keyerror,我的python代码中有一个keyError 0 我真的不明白这意味着什么,在我的情况下,我读了很多关于它,但我不能找到我自己的错误 谁能帮我找到它,也许能给我解释一下 问候, # use a function to pull all info from website def getdata(stock): # company quote group of items company_quote = requests.get(f"https://financialmodeli
# use a function to pull all info from website
def getdata(stock):
# company quote group of items
company_quote = requests.get(f"https://financialmodelingprep.com/api/v3/quote/{stock}")
company_quote = company_quote.json()
share_price = float("{0:.2f}".format(company_quote[0]['price']))
# balance sheet
BS = requests.get(f"https://financialmodelingprep.com/api/v3/financials/balance-sheet-statement/{stock}?period=quarter")
BS = BS.json()
# total debt
debt = float("{0:.2f}".format(float(BS['financials'][0]['Total debt'])/10**9))
# total cash
cash = float("{0:.2f}".format(float(BS['financials'][0]['Cash and short-term investments'])/10**9))
# income statement group of item
IS = requests.get(f"https://financialmodelingprep.com/api/v3/financials/income-statement/{stock}?period=quarter")
IS = IS.json()
# most recent quarterly revenue
qRev = float("{0:.2f}".format(float(IS['financials'][0]['Revenue'])/10**9))
# company profile group of items
company_info = requests.get(f"https://financialmodelingprep.com/api/v3/company/profile/{stock}")
company_info = company_info.json()
# CEO
ceo = company_info['profile']['ceo']
return (share_price, cash, debt, qRev, ceo)
tickers = ('AAPL', 'MSFT', 'GOOG', 'MVIS')
data = map(getdata, tickers)
# create the dataframe with pandas to store all of the info
df = pd.DataFrame(data, columns = ['Total Cash', 'Total Debt', 'Q3 2019 Revenue', 'CEO'], index = tickers)
print(df)
# writing to excel
writer = pd.ExcelWriter('example.xlsx')
df.to_excel(writer, 'Statistics')
writer.save()
我刚刚执行了您粘贴的代码,问题似乎是您没有正确使用API,似乎缺少API密钥,从您的代码中我得到以下信息:
{'Error Message':'无效的API密钥。请重试或访问我们的文档以创建一个免费的API密钥https://financialmodelingprep.com/developer/docs'}
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
getdata中第5行的文件“”
关键错误:0
因此,请查看API并发送正确的值(可能缺少标题等)您实际上没有添加任何代码供我们查看,只编写了“这是我的脚本”。我的错误在发布时变得很快,现在它被添加了