首页 > 编程语言 > 详细

Python进阶-pickle/eval/exec

时间:2018-08-01 10:18:09      阅读:147      评论:0      收藏:0      [点我收藏+]
  • 参考:
    •   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

 

Python进阶-pickle/eval/exec

原文:https://www.cnblogs.com/ryu-manager/p/9399453.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!