Posts: 14
Threads: 3
Joined: Jan 2020
Jan-18-2020, 03:40 AM
(This post was last modified: Jan-18-2020, 02:00 PM by buran.)
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!
Posts: 12,024
Threads: 484
Joined: Sep 2016
Jan-18-2020, 09:07 AM
(This post was last modified: Jan-18-2020, 09:08 AM by Larz60+.)
using f-string (python 3.6 or newer)
for x in read[0]:
Ticker = f"{x}"
Posts: 14
Threads: 3
Joined: Jan 2020
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
Posts: 8,155
Threads: 160
Joined: Sep 2016
Jan-18-2020, 01:48 PM
(This post was last modified: Jan-18-2020, 01:48 PM by buran.)
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).
Posts: 14
Threads: 3
Joined: Jan 2020
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
Posts: 8,155
Threads: 160
Joined: Sep 2016
Jan-18-2020, 01:55 PM
(This post was last modified: Jan-18-2020, 01:55 PM by buran.)
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?
Posts: 14
Threads: 3
Joined: Jan 2020
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!
Posts: 8,155
Threads: 160
Joined: Sep 2016
Jan-18-2020, 02:20 PM
(This post was last modified: Jan-18-2020, 02:21 PM by buran.)
don't pass header=None , so it will try to guess. or pass header=0 (i.e. first row)
Posts: 14
Threads: 3
Joined: Jan 2020
I removed header=None but it resulted in an error. Also, thanks for the guidance on the the use of tags
Posts: 8,155
Threads: 160
Joined: Sep 2016
(Jan-18-2020, 02:24 PM)coder1384 Wrote: but it resulted in an error what error?
|