pickle
- 将所有类型数据转化成二进制文件
存储
import pickle
my_list=[123,3.14,['anther list']]
pickle_file=open('my_list.pkl','wb')
pickle.dump(my_list,pickle_file)
pickle_file.close()
读取
pickle_file=open('my_list.pkl','rb')
my_list2=pickle.load(pickle_file)
print(my_list2)
[123, 3.14, ['anther list']]
Exception:
- AssertionError:断言为假时,抛出异常
- AttributeError:对象中没有这个成员函数
- IndexError:索引超出范围
- KeyError: 查找不存在的关键字
- SyntaxError: 语法错误
- TypeError:类型错误
- ZeroDivisionError:除数为0
mylist=['hhh']
assert len(mylist)>0
mylist.pop()
'hhh'
assert len(mylist)>0
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-14-9861f6d3a597> in <module>
----> 1 assert len(mylist)>0
AssertionError:
mylist.fishc()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-4984dcac7581> in <module>
----> 1 mylist.fishc()
AttributeError: 'list' object has no attribute 'fishc'
mylist=[1,2,3]
mylist[3]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-16-e6093086fa9c> in <module>
1 mylist=[1,2,3]
----> 2 mylist[3]
IndexError: list index out of range
my_dict={'one':1,'two':2,'three':3}
my_dict['one']
1
my_dict['four']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-18-c8be5b240d47> in <module>
----> 1 my_dict['four']
KeyError: 'four'
my_dict.get('four')#不会报错
print 'i love you'
File "<ipython-input-20-c9252a42fe03>", line 1
print 'i love you'
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('i love you')?
1+'2'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-22-7e3aa5c4fbef> in <module>
----> 1 1+'2'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
5/0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-23-0106664d39e8> in <module>
----> 1 5/0
ZeroDivisionError: division by zero
异常处理:
-
try-except:一旦出现异常,后面的语句将不会被执行
-
try-finally:无论怎样都必定会执行的语句。
-
raise:自己引发的异常
f=open('我是一个不存在的文件.txt')
print(f.read())
f.close()
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-24-6b8f65d6cfaf> in <module>
----> 1 f=open('我是一个不存在的文件.txt')
2 print(f.read())
3 f.close()
FileNotFoundError: [Errno 2] No such file or directory: '我是一个不存在的文件.txt'
try:
f=open('我是一个不存在的文件.txt')
print(f.read())
f.close()
except OSError:
print('文件出错啦')
文件出错啦
try:
sum=1+'2'
f=open('我是一个不存在的文件.txt')
print(f.read())
f.close()
except OSError as reason:
print('文件出错啦\n错误的原因是:'+str(reason))
except TypeError as reason:
print('类型出错啦\n错误的原因是:'+str(reason))
类型出错啦
错误的原因是:unsupported operand type(s) for +: 'int' and 'str'
try:
int('abc')
sum=1+'2'
f=open('我是一个不存在的文件.txt')
print(f.read())
f.close()
except:
print('出错啦')
出错啦
try:
sum=1+'2'
f=open('我是一个不存在的文件.txt')
print(f.read())
f.close()
except (OSError,TypeError):
print('出错啦')
出错啦
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-34-2946b391f382> in <module>
6 print('出错啦')
7 finally:
----> 8 f.close()
NameError: name 'f' is not defined
try:
f=open('我是一个存在的文件.txt','w')
print(f.write('我存在了'))
except (OSError,TypeError):
print('出错啦')
finally:
f.close()
4
raise
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-38-9c9a2cba73bf> in <module>
----> 1 raise
RuntimeError: No active exception to reraise
raise ZeroDivisionError('除数为0的异常')
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-40-bec5ebf736a5> in <module>
----> 1 raise ZeroDivisionError('除数为0的异常')
ZeroDivisionError: 除数为0的异常