I am trying to write a code that will utilize an api to get data from yahoo finance and then export that data to an excel spreadsheet. The code worked fine before I added the last line that's supposed to export to excel. I keep getting the following error message "str object has no attribute 'to_excel'".
import yfinance as yf
from pandas import DataFrame
#opens txt document that has stock ticker symbols
fileref = open("a.txt","r")
listOfLines = fileref.readlines()
listOfLines = map(lambda line: line.strip(), listOfLines)
fileref.close()
ticker_symbols = listOfLines
#loop over each element and store it in the variable ticker
for ticker in ticker_symbols:
ticker_query = yf.Ticker(ticker)
price_to_book = ticker_query.get_info()["priceToBook"]
industry = ticker_query.get_info()["industry"]
dividend = ticker_query.get_info()["dividendYield"]
trailingPe = ticker_query.get_info()["trailingPE"]
dataset = (ticker + " : " + str(price_to_book) + " " + str(industry) + " " + str(dividend) + " " + str(trailingPe))
dataset.to_excel("x.xlsx")
Can anyone offer any help?
Ross