首页 > 解决方案 > 在 python 代码中出现错误:“RuntimeError: No active exception to reraise”

问题描述

如果数字不在 1 到 15 之间,我有下面的代码应该引发异常。相反,如果我选择 1 到 15 以外的数字,我会收到以下错误消息。

错误消息:“RuntimeError:没有要重新引发的活动异常”

如果在错误的地方使用“尝试”和“除外”,有人可以指导我。

这是我的代码:

import pandas as pd
from txt_preprocessing.text_preprocessing import TextPreprocessor as TP


def preprocessing_util_func(csv_file,usr_lst,language='english'):
    df = pd.read_csv(csv_file)
    lstt = usr_lst
    print(lstt)
    try:
        for i in lstt:
            if i==1:
                df['X']=df['X'].apply(lambda x: TP.tokenize_doc(x,return_var_type="list_tokens"))
            elif i==2:
                df['X']=df['X'].apply(lambda x: TP.remove_stop_words(x,return_var_type="string"))
            elif i==3:
                df['X'] = df['X'].apply(lambda x: TP.remove_punctuation(x, return_var_type="string"))
            elif i==4:
                df['X']=df['X'].apply(lambda x: TP.sentencize_doc(x,return_type="list_sentences"))
            elif i==5:
                df['X']=df['X'].apply(lambda x: TP.custom_sentencize_doc(x,return_type="list_sentences"))
            elif i==6:
                df['X']=df['X'].apply(lambda x: TP.case_fold(x))
            elif i==7:
                df['X']=df['X'].apply(lambda x: TP.custom_tokenize_doc(x))
            elif i==8:
                df['X']=df['X'].apply(lambda x: TP.lemmatize_doc(x))
            elif i==9:
                df['X']=df['X'].apply(lambda x: TP.remove_special_characters(x))
            elif i==10:
                df['X']=df['X'].apply(lambda x: TP.remove_non_ascii_characters(x))
            elif i==11:
                df['X']=df['X'].apply(lambda x: TP.remove_custom_stop_words(x,return_var_type="list_tokens"))
            elif i==12:
                df['X']=df['X'].apply(lambda x: TP.remove_numbers(x,return_var_type="list_tokens"))
            elif i==13:
                df['X']=df['X'].apply(lambda x: TP.remove_whitespace(x))
            elif i==14:
                df['X']=df['X'].apply(lambda x: TP.remove_custom_stopphrases(x,custom_stopphrases=['is not','to not']))
            elif i==15:
                df['X']=df['X'].apply(lambda x: TP.remove_custom_portion_text(x))
        except:
            print("Please check the number under lst selection", sys.exc_info()[0])
        raise
        return df

标签: python

解决方案


是的,它有一些错误。最后,它应该是:

except:
    print("Please check the number under lst selection",sys.exc_info()[0])
    raise
return df

推荐阅读