Python进阶-pickle/eval/exec

  • 参考:
    •   eval/exec/compile的区别:https://stackoverflow.com/questions/2220699/whats-the-difference-between-eval-exec-and-compile-in-python
    •   pickle的使用: https://pythontips.com/2013/08/02/what-is-pickle-in-python/
  • 背景
    • 之前用打印到文件,逐行读出用eval转换格式。现在用pickle可直接实现。
  • pickle_eval_exec的区别:
    • pickle: dump到file, 从file中load
    • eval: eval accepts only a single expression, eval returns the value of the given expression。
    • exec: exec can take a code block that has Python statements: loops, try: except:class and function/method definitions and so on. return None.
  • Pickle的使用:(pickle.load(), pickle.dump())
import pickle

a = ['test value','test value 2','test value 3']
a
['test value','test value 2','test value 3']

file_Name = "testfile"
# open the file for writing
fileObject = open(file_Name,'wb') 

# this writes the object a to the
# file named 'testfile'
pickle.dump(a,fileObject)   

# here we close the fileObject
fileObject.close()
# we open the file for reading
fileObject = open(file_Name,'r')  
# load the object from the file into var b
b = pickle.load(fileObject)  
b
['test value','test value 2','test value 3']
a==b
True

 

转载于:https://www.cnblogs.com/ryu-manager/p/9399453.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值