I'm new to python (and StackOverflow) and using a series of csv data requests to an API to learn
My initial script looped through a list of stock ticker symbols to modify the API URL and it works well:
import pandas as pd
#set the list of tickers to request csv
tick = ['nke','ibm','mmm','msft']
for t in tick:
#print the url to make sure its correct
print("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" + t + "&apikey=demo&datatype=csv")
#send the csv file request to server
df = pd.read_csv("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" + t + "&apikey=demo&datatype=csv")
#option to show data in data frame to troubleshoot if needed
#print(df)
# Write DataFrame to CSV File using tick as file name modifier
df.to_csv("data-" + t + "-test-data-20221006.csv")
Next I'm trying to scale this a bit by reading a one-column .txt file to use as the list of variables for the script, but I can't seem to get it to work:
import pandas as pd
# use txt file list of tickers symbols
# opening the file in read mode
my_file = open("tickertest.txt", "r")
# reading the file
data = my_file.read()
# replacing end splitting the text
# when newline ('\n') is seen
data_into_list = data.split("\n")
print(data_into_list)
my_file.close()
#set the list of tickers to request csv
tick = [data_into_list]
for t in tick:
#print the url to make sure its correct
print("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" + t + "&apikey=demo&datatype=csv")
#send the csv file request to server
df = pd.read_csv("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" + t + "&apikey=demo&datatype=csv")
#option to show data in data frame to troubleshoot if needed
#print(df)
# Write DataFrame to CSV File using tick as file name modifier
df.to_csv("data-" + t + "-test-data-20221006.csv")
I'd be most grateful for any feedback on where my mistake is in this script
BTW---The text file is as follows:
nke ibm mmm msft
Thank you!