Python Forum
Object attribute behavior different in 2 scripts - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Object attribute behavior different in 2 scripts (/thread-40173.html)



Object attribute behavior different in 2 scripts - db042190 - Jun-13-2023

Hi, in scripts 1 and 2 below I got different behavior on object yf.Ticker.

Script 1 runs fine and in fact so too did a similar Script 2 for a moderator from this forum. But on my PC, Script 2 generates the attribute error you see in item 3. I did learn that I can just set ticker to line.strip("\n\r") but I think I want to understand the attribute error anyway.

import yfinance as yf
import sys

for line in open('C:\\Users\\stant\\OneDrive\\Documents\\daxandpython\\tickers.txt'):
    tkr=yf.Ticker(line.strip("\n\r"))
    with open('C:\\Users\\stant\\OneDrive\\Documents\\daxandpython\\yfinance\\'+line.strip("\n\r") +'.txt', 'w') as sys.stdout:
         tkr.history(period="max")
import yfinance as yf
import pandas as pd
#import sys
from datetime import date
today = date.today()
data = None
for line in open('C:\\Users\\stant\\OneDrive\\Documents\\daxandpython\\tickers.txt'):
    ticker=yf.Ticker(line.strip("\n\r"))
    x = yf.download(ticker, start="2017-01-01", end=today, progress=False).round(2)
    #x["Stock"]=ticker
    x.insert(0, "Ticker", ticker)
    if data is None:
        data = x
    else:
        data = pd.concat((data, x))
    data = data.sort_index()
Error:
Traceback (most recent call last): File "<stdin>", line 3, in <module> File "C:\Users\stant\AppData\Local\Programs\Python\Python311\Lib\site-packages\yfinance\multi.py", line 107, in download tickers, (list, set, tuple)) else tickers.replace(',', ' ').split() ^^^^^^^^^^^^^^^ AttributeError: 'Ticker' object has no attribute 'replace'



RE: Object attribute behavior different in 2 scripts - deanhystad - Jun-14-2023

In the first example you don't use tkr (a yf.Ticker object). In the second you pass a yf.Ticker object to yf.download(). yf.download() expects tickers to be strings, not yf.Ticker objects.

I looked at your other threads. This is the first time yf.Ticker makes an appearance. This is all on you. It is not a fault of the modules or the GUI you are using. This is a sloppy programming, lack of attention to detail error. We've all had those and had to learn to have "fresh eyes" when reading code. You need to read the actual code instead of the code you expect to see. You should have seen that yf.Ticker does not appear in the moderator's script and that would make you research what yf.Ticker is, and what arguments are expected by yf.download. From that you would know that yf.Ticker is not the correct type for ticker arguments passed to yf.download.