51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

python 出现错误 继续运行 (try catch)

1=

try:
    # 这里是可能会出错的代码
    risky_operation()
except Exception as e:
    # 这里处理错误,比如打印错误信息,或者记录日志
    print(f"An error occurred: {e}")
    # 接着执行其他代码
finally:
    # 这里的代码无论是否出现异常都会执行
    cleanup_operation()
 
# 继续执行其他代码
continue_running_code()

如果是for循环中, 需要用 continue 来继续执行。

例如:

def get_all_XiaoV_from_Qiang_QuShi(localpath1):
    info1=  '--小V  start--   '
    writeLog_to_txt_nocode(info1)
    path = BASE_DIR + '/jishu_stock/stockdata/qiang_qushi_stocks.csv'
    # path =  '/app/stock/stock/jishu_stock/stockdata/qiang_qushi_stocks.csv'
    data = pd.read_csv(path, dtype={'code': str})
    for index, row in data.iterrows():
        # print row['ts_code']

        stock_code = row['代码']
        # stock_code = row['ts_code']
        # print row
        stock_code = getSockCode_from_SZSH601899(stock_code)
        stockdata_path = BASE_DIR + localpath1 + stock_code + ".csv"
        # print stockdata_path
        try:
           df = pd.read_csv(stockdata_path, index_col=0)

        except Exception as e:
            # `e` has the error info
            print `e`
            continue



        data6_1 = df.iloc[0:130]  # 前6行
        # data6_1 = df.iloc[20:32]  # 前6行
        len1 = len(data6_1)
        isAn_XiaoV_model(data6_1, stock_code)
赞(7)
未经允许不得转载:工具盒子 » python 出现错误 继续运行 (try catch)