python - 在 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
解决方案
是的,它有一些错误。最后,它应该是:
except:
print("Please check the number under lst selection",sys.exc_info()[0])
raise
return df
推荐阅读
- django - 由于从“/”重定向到“https://testserver/”,在我的生产环境中获得 301 而不是 200
- swift - 如何在 Swift 中使用 OCaml 库?
- java - 如何获取 IBM Java 8 的调试信息包
- r - 如何在 R 中使用库 DT 渲染 DataTable 时使用行条件为单元格着色?
- string - 从给定双精度返回字符串的方法
- android - Python - AndroidViewClient 以高 FPS 拍摄快照
- r - 重命名 phylo 提示标签
- java - java计算器中需要小数
- c# - 并非所有 JSON 属性都发送到服务器
- angular - 带有 Bootstrap 4.1.3 的 Angular6 中的下拉菜单不起作用