首页 > 解决方案 > 股票代码列表的属性错误

问题描述

我对为什么会收到属性错误感到有些困惑。仅当我放置等于 stock_list 的列表时才会发生此错误。如果我打印列表然后复制并粘贴我没有收到错误/

我尝试从代码顶部输入技术代码,但是尝试时出现属性错误,当我打印列表然后复制和粘贴时不会发生这种情况,应该是同一件事吗?

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”对象没有属性“replace”

标签: pythonpython-3.xlistfinance

解决方案


检查您的实施

Technicals.get('股本回报率',0)

如果键不存在,方法get(对于 type dict)将返回默认值。0并且通过您的实现,所有默认值都有一个 type int。因为它们被设置为数字,而不是字符串(用引号括起来)。

如果零是正确的默认值,您可以忽略类型更改的错误并保留您的实现。

Technicals.get('股本回报率', '0')


推荐阅读