Python Forum

Full Version: How do you put quotations around a result from a for loop?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello folks,

I am attempting to have the for loop iteration result in quotations so that it passes to another function within the loop. Here is my code along with further explanation of what I'm attempting to do:

read = pd.read_csv('Stocks.csv', header=None, delimiter=',')

for x in read[0]:
     
    Ticker = x   #I need the x iteration to have quotations around the result to pass into the results = si.get_quote_table(x, dict_result=False) import
   
     
    results = si.get_quote_table(x, dict_result=False) #The x in the parenthesis represents a stock ticker symbol from the csv file within the for loop.  This will bring back data on the stock.  It requires having quotations around the input for the si.get_quote_table to respond.
    
    results.to_csv('Stockdata.csv', index=False)
I would greatly appreciate any help/guidance someone can provide.

Thank you in advance!
using f-string (python 3.6 or newer)
for x in read[0]:
    Ticker = f"{x}"
Hi Larz60+, thanks for the quick reply to my post. I tried using the f-string as you suggested but unfortunately I'm getting the following error:
ValueError: No tables found.

It seems that when I use an input function it works fine referencing the variable "Ticker" in the si.get_quote_table. Any thoughts on why it's not passing through properly even though an input function with prompting the typing of the ticker does pass through?

Thanks again!
AJ
looking at this:
https://www.r-bloggers.com/how-to-get-li...th-python/
and http://theautomatic.net/yahoo_fin-documentation/
you don't need quotes. just pass ticker as str. assuming x is str your code
results = si.get_quote_table(x, dict_result=False)
should work. if not, the problem is somewhere else, not the quotes (or lack of them).
Thanks buran, I'm getting the following error referencing the si.get_Quote_Table(x, dict_result=False) line:

ValueError: No tables found

But when I have an input function where I'm prompted for the ticker, it passes through correctly and responds with the data per that ticker. But when the variable is used, it seems to not recognize it

Any thoughts or work around?

Thanks again,
AJ
print the variable before using it, so you can see what it is. even better print(repr(x))
we don't have your input data
is it possible that your Stocks.csv has header?
Hi buran, I printed x and found out that the first row of my csv is "Symbol" as a header and it is causing the issue. How do I go about skipping the first row or indicating that their is a header row in my code so that the iteration begins on row two? Thanks again!
don't pass header=None, so it will try to guess. or pass header=0 (i.e. first row)
I removed header=None but it resulted in an error. Also, thanks for the guidance on the the use of tags
(Jan-18-2020, 02:24 PM)coder1384 Wrote: [ -> ]but it resulted in an error
what error?
Pages: 1 2