Search code examples
pythonpython-3.xlistfinance

Attribute Error with List of Stock Tickers


I am a bit confused as to why I am getting attribute error. This error only occurs when when I put a list equal to stock_list. If I print the list then copy and paste I do not get the error/

I have tried to input tech tickers from the top of the code, but I get attribute error when I try, which doesn't happen when I have the list print then copy and paste, which should be the same thing?

file = 'techtickerlist.csv'
with open(file) as f:
    reader = csv.reader(f)
    technologyTickers = []
    for row in reader:
        technologyTickers.append(row[0])

def scrape(stock_list, interested, technicals):
    SuggestedStocks = []
    for each_stock in stock_list:
        try:
            technicals = scrape_yahoo(each_stock)
            condition_1 = float(technicals.get('Return on Equity',0).replace('%','').replace('N/A','-100').replace(',','')) > 25
            condition_2 = float(technicals.get('Trailing P/E',0).replace('N/A','0').replace(',','')) < 25
            condition_3 = float(technicals.get('Price/Book',0).replace('N/A','100')) <8
            condition_4 = float(technicals.get('Beta (3Y Monthly)',0).replace('N/A','100')) <1.1
            if condition_1 and condition_2 and condition_3 and condition_4:
                print(each_stock)
                SuggestedStocks.append(each_stock)  
                for ind in interested: 
                    print(ind + ": "+ technicals[ind])         
                print("------")
                time.sleep(1)   
        except ValueError:
                print('Value Error')
                return
                                              # Use delay to avoid getting flagged as bot
    #return technicals
    print(SuggestedStocks)


def main():

    stock_list = technologyTickers
    interested = ['Return on Equity', 'Revenue', 'Quarterly Revenue Growth','Trailing P/E', 'Beta (3Y Monthly)','Price/Book']
    technicals = {}

    tech = scrape(stock_list, interested, technicals)
    print(tech)

AttributeError: 'int' object has no attribute 'replace'


Solution

  • Check your implementation

    technicals.get('Return on Equity',0)

    Method get (for type dict) will return default value 0 if the key is not present. And by your implemenation all default values have a type int. Because they were set as number, not a string (wrapped with quotation signs).

    If zero is a correct default value you may omit error with type changing and keep your implemetation.

    technicals.get('Return on Equity', '0')