我正在尝试进行股票筛选。对于某些股票,它没有值,并且显示“ N / A”,我想将其替换为0。此外,对于同一指标,它具有百分比字符,我也需要删除该字符。我在从字典的相同值中删除两个字符时遇到了麻烦。
例如,在所有情况下,我都需要摆脱百分号,而在少数情况下,则需要摆脱不适用。
...
def scrape(stock_list, interested, technicals):
for each_stock in stock_list:
technicals = scrape_yahoo(each_stock)
condition_1 = float(technicals.get('Return on Equity',0).replace('%','')) > 25
condition_2 = float(technicals.get('Trailing P/E',0)) > 15
condition_3 = float(technicals.get('Price/Book (mrq)',0)) <15
print(each_stock)
for ind in interested:
if (condition_1 and condition_2)==True:
print(ind + ": "+ technicals[ind])
print("------")
time.sleep(1) # Use delay to avoid getting flagged as bot
return technicals
...
ValueError:无法将字符串转换为浮点数:“ 54.34%”