0

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!

chorink65
  • 29
  • 5
  • Have a look at using python `with` keyword for opening/reading a file. This link might be a useful reference: https://stackoverflow.com/questions/38105507/when-should-i-ever-use-file-read-or-file-readlines – j_b Oct 06 '22 at 16:58

1 Answers1

0

With help from @j_b and some additional exploration including this article: https://www.delftstack.com/howto/python/python-readlines-without-newline/ I got the following script to work for my task:

import pandas as pd

#set the list of tickers to request csv

#tick = ['nke','ibm','mmm','msft']

#open txt file list of ticker symbols to use for URL loop

with open('tickertest.txt', 'r') as my_file:
    newline_break = ""
    for readline in my_file:
        line_strip = readline.strip()
        newline_break += line_strip

#for t in tick:

#print the url to make sure its correct
        print("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" + line_strip + "&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=" + line_strip + "&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-" + line_strip + "-test-data-20221006.csv")

Thank you!

chorink65
  • 29
  • 5