python - 股票代码列表的属性错误
问题描述
我对为什么会收到属性错误感到有些困惑。仅当我放置等于 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”
解决方案
检查您的实施
Technicals.get('股本回报率',0)
如果键不存在,方法get
(对于 type dict
)将返回默认值。0
并且通过您的实现,所有默认值都有一个 type int
。因为它们被设置为数字,而不是字符串(用引号括起来)。
如果零是正确的默认值,您可以忽略类型更改的错误并保留您的实现。
Technicals.get('股本回报率', '0')
推荐阅读
- pharo - Pharo ZnClient 可以托管多个服务器实例吗?
- ios - React Invariant Violation:最小化 React 错误 #267
- python - 使用 subprocess.Popen() 从 python 脚本实时输出
- php - Laravel hasMany 不返回集合
- c# - sql 命令产生在 sql server 中不为空的空结果
- wordpress - 无法删除或编辑类别 - Wordpress
- api - DocuSign 未过期的 OAuth 令牌
- sql-server - 如何将一个表中的唯一编号分配给另一个表行并更改 sql server 中的标志
- html - 星级评分似乎完全错误
- java - 为 Oracle DomParser 解析 XXE