1. Exception handling process
Exception Name: | describe |
---|---|
BaseException | Base class for all exceptions |
SystemExit | Interpreter request exit |
KeyboardInterrupt | User interrupts execution (usually enter ^ C) |
Exception | Base class for general error |
StopIteration | The iterator has no more values |
GeneratorExit | An exception occurs in the generator to notify the exit |
StandardError | Base class for all built-in standard exceptions |
ArithmeticError | Base class for all numeric calculation errors |
FloatingPointError | Floating point calculation error |
OverflowError | Numeric operation exceeds maximum limit |
ZeroDivisionError | Divide (or modulo) zero (all data types) |
AssertionError | Assertion statement failed |
AttributeError | Object does not have this property |
EOFError | No built-in input, reaching EOF mark |
EnvironmentError | Base class for operating system error |
IOError | Input / output operation failed |
OSError | Operating system error |
WindowsError | system call filed |
ImportError | Failed to import module / object |
LookupError | Invalid base class for data query |
IndexError | This index does not exist in the sequence |
KeyError | This key does not exist in the map |
MemoryError | Memory overflow error (not fatal for Python interpreter) |
NameError | Object not declared / initialized (no properties) |
ReferenceError | A weak reference attempts to access an object that has been garbage collected |
RuntimeError | General runtime error |
NotImplementedError | Methods not yet implemented |
SyntaxError | Python syntax error |
IndentationError | Indent error |
TabError | Mixed use of Tab and space |
SystemError | General interpreter system error |
TypeError | Invalid operation for type '' |
ValueError | Invalid parameter passed in |
UnicodeError | Unicode related errors |
UnicodeDecodeError | Error in Unicode decoding |
UnicodeEncodeError | Error encoding Unicode |
UnicodeTranslateError | Error in Unicode conversion |
Warning | Warning base class |
DeprecationWarning | Warning about deprecated features |
FutureWarning | Warning about future semantic changes in construction |
OverflowWarning | Old warning about auto promotion to long |
PendingDeprecationWarning | Warning that the feature will be discarded |
RuntimeWarning | Warning of suspicious runtime behavior |
SyntaxWarning | Warning of suspicious syntax |
UserWarning | Warning generated by user code |
- AttributeError attempts to access a tree without an object, such as foo.x, but foo has no attribute x
- IOError input / output exception; Basically, the file cannot be opened
- ImportError failed to import module or package; Basically, it is a path problem or a name error
- Indementerror syntax error (subclass of); The code is not aligned correctly
- IndexError subscript index exceeds the sequence boundary. For example, when x has only three elements, it attempts to access x[5]
- KeyError attempted to access a key that does not exist in the dictionary
- KeyboardInterrupt Ctrl+C pressed
- NameError uses a variable that has not been assigned to an object
- SyntaxError Python code is illegal and cannot be compiled (I think this is a syntax error and wrong writing)
- TypeError: the type of the incoming object does not meet the requirements
- UnboundLocalError attempts to access a local variable that has not been set. Basically, you think you are accessing it because there is another global variable with the same name
- ValueError passes in a value that the caller does not expect, even if the value type is correct
2. Exception handling method
2.1 single exception handling
The syntax is as follows:
try: code #Processed statements except Error1 as e: #When encountering Error1, execute the following statement and write it as exception Error1, e in python2 print(e)
demo
name = [1,2,3] try: name[3] #The subscript value 3 does not exist except IndexError as e: #Grab the IndexError exception print(e) #e is the details of the error
#Output
list index out of range
2.2 multiple exception handling
The syntax is as follows:
try: code except Error1 as e: #Handling Error1 exception print(e) except Error2 as e: #Handling Error2 exception print(e)
The code is as follows:
name = [1,2,3] data = {"a":"b"} try: data["c"] #An exception KeyError has occurred here, so jump out of the code directly and go to KeyError for processing name[3] except IndexError as e: print(e) except KeyError as e: print(e)
#Output
'c'
Merge into one except ion, with the syntax as follows:
try: code except (Error1,Error2,...) as e: print(e)
try: data["c"] name[3] except (IndexError,KeyError) as e: print(e)
#Output
'c'
Note: the use of the second writing method: all errors in parentheses, no matter what kind of errors occur, will be handled in a unified way.
2.3 Exception
try: code except (Error1,Error2,...) as e: print(e) except Exception as e: #Use Exception to catch all exceptions at once. In general, it is recommended to use it at the end of the Exception and catch unknown exceptions at the end print(e)
demo
try: open("qigao.text","r",encoding="utf-8") except (IndexError,KeyError) as e: #There are no indexerror and keyerror exceptions print(e) except Exception as e: #Only through this Exception handling, Exception can catch all exceptions print(e) #output [Errno 2] No such file or directory: 'qigao.text'
2.4 use of else
Function: if there is no exception, follow the logic code of the else part
try: print("qigao,handson") #The code has no exception except (IndexError,KeyError) as e: print(e) except Exception as e: print(e) else: #There is no exception error, follow the logic code of else print("No exception") #output qigao,handson No exception
2.5 finally
try: code except (Error1,Error2,...) as e: print(e) except Exception as e: print(e) else: print("No error, execute") finnally: print("Whether there is a mistake or not, I will carry it out finnally")
2.6 custom exception
class GaoError(Exception): #Define an Exception class and inherit Exception def __init__(self,message): self.message = message def __str__(self): return self.message #Give the object a name
try: raise GaoError("The database cannot be connected") #Trigger custom exception, GaoError("database connection failed") object except GaoError as e: print(e)
Trigger custom exception:
2.7 assertion
Assertions are used as your next program execution. If the later program depends on the previous program, the latter program is very important, that is, the later program must not be executed incorrectly. Therefore, check before execution.
class C(object): def __init__(self): self.name = "AAAAA" c_obj = C() assert c_obj.name == "AAAAA" #Assert print("No errors continue...") #output No errors continue
Assertion does not match
class C(object): def __init__(self): self.name = "AAAAA" c_obj = C() assert c_obj.name == "BBBBB" #String mismatch asserted print("No errors continue...") Traceback (most recent call last): File "E:/PycharmProjects/pytest/day7/Assert.py", line 10, in <module> assert c_obj.name == "BBBBB AssertionError #Report assertion exception error
3. Regularize and replace abnormal values
#Output result integration: import json from pprint import pprint import json import pandas as pd enddate=timeUtils().getAnyDay(-1) input_path= result_path = output_path = def get_textLine(path): string_list = [] string_list1 = [] file_data = pd.read_table(path,sep = r'\001',encoding="UTF-8") for index, elem in file_data.iterrows(): string_list.append(elem[0]) string_list1.append(elem[1]) return string_list,string_list1 def get_textLine1(path): string_list = [] file_data = pd.read_table(path,sep = r'\001',encoding="UTF-8") for index, elem in file_data.iterrows(): string_list.append(elem[0]) return string_list data_input,label=get_textLine(input_path) data_result=get_textLine1(result_path) # print(data_input[:3])#View intermediate results print(data_result[200:300]) # print(len(data_result)) # pred =re.sub("\[\{\'text\': \'([0-9])\', \'probability\': (0.[0-9]+)}]",r'\1<-\2', text) cases = [] # cases1 = [] for i in range(len(data_result)): try: if data_result[i]=='\"\"': index=str(0) # index1=str(0) #Exception: temporarily replace with 0 else: pred =re.sub("\[\{\'text\': \'([0-9])\', \'probability\': (0.[0-9]+)}]",r'\1<-\2', data_result[i]) #'\[\{\'text\': \'([0-9+])\'',r'\1' index=pred[0] # index1=pred[3:] except IndexError as e: index = 'UNK' # cases.append(f'{data_input[i]}\001{label[i]}\001{pred}') cases.append(f'{index}') # cases1.append(f'{index1}') # final_data=pd.DataFrame(columns = ['content','label','pred','probability']) final_data=pd.DataFrame(columns = ['content','label','pred']) final_data['content']=data_input final_data['label']=label final_data['pred']=cases # final_data['probability']=cases1 # final_data.to_csv(output_path, sep='\001',index=False,header=False) print(final_data[250:300]) # final_data.to_csv(output_path, sep='\001',index=False,index_label=False,header=False) #View results # print(final_data) # test=pd.read_csv(output_path,encoding="UTF-8",sep = r'\001',index_col="content") # test=pd.read_csv(output_path,encoding="UTF-8",sep = r'\001') # print(test.head(30)) # with open(output_path, 'w', encoding='utf8')as f: # for case in cases: # print(case) #View output # f.write("\n".join(case)) print("Data uploaded oss")