小甲鱼教学笔记——pickle:腌制+Exception:异常处理

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的异常
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值