Python代码未列出正确的输出,未显示错误

nxowjjhe  于 2023-04-10  发布在  Python
关注(0)|答案(1)|浏览(112)

我正在创建一个筛选过程,为我正在做的一个uni项目寻找合格的股票,但当我运行此代码时,我得到的是以下输出,而不是股票列表:
runcell(0,'C:/Users/cades/Downloads/untitled3.py')空DataFrame列:[股票代码,earnings_to_equity,debt_to_equity,quick_ratio]指数:[]

import yfinance as yf
import pandas as pd

tickers = yf.Tickers('')

financial_data = pd.DataFrame(columns=['ticker', 'earnings_to_equity', 'debt_to_equity', 'quick_ratio'])

for ticker in tickers.tickers:
        balance_sheet = ticker.balance_sheet
        income_statement = ticker.income_statement
        
        earnings_to_equity = income_statement.loc['Net Income'] / balance_sheet.loc['Total Stockholder Equity']
        debt_to_equity = balance_sheet.loc['Total Debt'] / balance_sheet.loc['Total Stockholder Equity']
        quick_ratio = (balance_sheet.loc['Total Current Assets'] - balance_sheet.loc['Inventory']) / balance_sheet.loc['Total Current Liabilities']

        
        if debt_to_equity < 2 and quick_ratio >= 1.5 and earnings_to_equity > 0:
            financial_data = financial_data.append({'ticker': ticker.ticker, 'earnings_to_equity': earnings_to_equity, 'debt_to_equity': debt_to_equity, 'quick_ratio': quick_ratio}, ignore_index=True)
            
        
        if len(financial_data) >= 100:
            break


print(financial_data)
osh3o9ms

osh3o9ms1#

  • 我会在这里回答这个问题,因为这将有助于未来的开发者寻找同样的东西。我会深入为什么你不应该做除了通过。*

try/except表达式捕捉代码中发生在它们之间的错误,如果发生了异常,则返回一个对象Exception。
在你的代码中,在运行try之后,except正在捕获异常,然而,你只是在传递,而不是对它做些什么,来纠正或处理错误。甚至没有显示msg。这几乎等同于把污垢放在地毯下的编程。你必须避免这种情况,即使是在“只是现在的代码”中,因为你只会搞乱你未来的自己:调试器的家伙。
相反,根据你希望你的代码有多干净(尝试做得更干净,但如果你正在尝试一些事情,你可能想保存一些时间,这是可以的),你可以尝试这种方式:

  • 看一个python的回溯,非常粗糙,它不处理任何事情,但是让我们看看错误是这样做的:
try:
    my_code()
except Exception as e: # in here you catch whereever exception happens
    raise e # then you don pass, you re raise it, so you can see what's happening
  • 一旦你识别出错误(在你的例子中,Pandas列为空,你就可以捕获它,就像这样。允许你根据错误类型做不同的事情。例如:如果panda错误,处理panda错误,如果API错误,处理api错误等等。你可以根据类型对它们进行分组,参见python异常文档来了解如何操作。
try:
    my_code()
except (APIError, EmptyDataFrameColumns) as e: # in here you catch these two types
    code_that_handles_data_errors() #then you code what you need in those 2 cases
except Exception as e: # in here you catch the other exceptions
    raise e # then you re raise it, so you can see what's happening when you get a new exception you didn't know.
  • 你也可以记录python返回的error对象中的消息:
try:
    my_code()
except (APIError, EmptyDataFrameColumns) as e: # in here you catch these two types
    log.warning(f"Something went wrong with the external data. Here is the error: {e.messsage}")
    code_that_handles_data_errors() #then you code what you need in those 2 cases
except Exception as e: # in here you catch the other exceptions
    log.error(f"Something weird happened. Here is the error: {e.messsage}")
    #you only log here, bc you don't know the error, but you need to see it to be able to handle it for the future, reading the logs.

开始的时候,我通常在这些状态之间循环。从不除了通过。只要有可能,分类和处理已知的错误

相关问题