Python数据编组对文字串的读写

开发 后端
Python数据编组是一种在国际上应用也相当广泛的计算机语言,下面的文章就是对其相关实际应用方案的具体介绍,忘、希望对你会有所帮助。

如果你对Python数据编组这种计算机语言有不解之处时,或想了解Python数据编组的实际相关应用方案时,你可以浏览我们的文章,希望我们的文章就是对你会有所收获。以下是文章的具体介绍。

使用前一节中介绍的模块,可以实现在文件中对字符串的读写。然而,有的时候,需要传递其它类型的数据。如list、tuple、dictionary和其它对象。在Python数据编组中,你可以使用Pickling来完成。你可以使用Python标准库中的“pickle”模块完成数据编组。下面,我们来编组一个包含字符串和数字的list:

  1. view plaincopy to clipboardprint?  
  2. import pickle   
  3.  
  4. fileHandle = open ( 'pickleFile.txt', 'w' )   
  5. testList = [ 'This', 2, 'is', 1, 'a', 0, 'test.' ]   
  6. pickle.dump ( testList, fileHandle )   
  7. fileHandle.close()   
  8.  
  9. import pickle  
  10.  
  11. fileHandle = open ( 'pickleFile.txt', 'w' )  
  12. testList = [ 'This', 2, 'is', 1, 'a', 0, 'test.' ]  
  13. pickle.dump ( testList, fileHandle )  
  14. fileHandle.close()  

拆分编组同样不难:

  1. view plaincopy to clipboardprint?  
  2. import pickle   
  3.  
  4. fileHandle = open ( 'pickleFile.txt' )   
  5. testList = pickle.load ( fileHandle )   
  6. fileHandle.close()   
  7.  
  8. import pickle  
  9.  
  10. fileHandle = open ( 'pickleFile.txt' )  
  11. testList = pickle.load ( fileHandle )  
  12. fileHandle.close()  
  13.  

 

现在Python数据编组试试存储更加复杂的数据:

  1. view plaincopy to clipboardprint?  
  2. import pickle   
  3.  
  4. fileHandle = open ( 'pickleFile.txt', 'w' )   
  5. testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson',
     [ 1, 2, 7 ] ]   
  6. pickle.dump ( testList, fileHandle )   
  7. fileHandle.close()   
  8.  
  9. import pickle  
  10.  
  11. fileHandle = open ( 'pickleFile.txt', 'w' )  
  12. testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson', 

    [ 1, 2, 7 ] ]  
  13. pickle.dump ( testList, fileHandle )  
  14. fileHandle.close()view plaincopy to clipboardprint?  
  15. import pickle   
  16.  
  17. fileHandle = open ( 'pickleFile.txt' )   
  18. testList = pickle.load ( fileHandle )   
  19. fileHandle.close()   
  20.  
  21. import pickle  
  22.  
  23. fileHandle = open ( 'pickleFile.txt' )  
  24. testList = pickle.load ( fileHandle )  
  25. fileHandle.close()   
  26.  

如上所述,使用Python数据编组的“pickle”模块编组确实很简单。众多对象可以通过它来存储到文件中。如果可以的话,“cPickle”同样胜任这个工作。它和“pickle”模块一样,但是速度更快:

  1. view plaincopy to clipboardprint?  
  2. import cPickle   
  3.  
  4. fileHandle = open ( 'pickleFile.txt', 'w' )   
  5. cPickle.dump ( 1776, fileHandle )   
  6. fileHandle.close()   

以上是对Python数据编组实际应用的相关内容的部分介绍。
 

责任编辑:佚名 来源: linux.chinaitlab.com
相关推荐

2017-08-01 17:34:47

Linux内核驱动文件读写

2010-03-12 15:49:46

Python字串查找

2020-01-09 10:47:15

HDFS数据文件

2010-04-30 11:22:23

Unix系统

2021-08-05 10:00:02

Python编程语言

2010-03-05 09:40:08

Python递归

2010-07-09 17:07:14

SQL Server数

2019-11-19 11:20:25

Python数据结构Windows

2021-02-26 20:55:56

JavaNIO随机

2022-11-07 07:04:25

2010-07-12 08:36:35

SQL Server数

2021-07-20 15:42:05

编程语言PythonJava

2009-07-15 16:42:03

iBATIS读写CLO

2009-12-02 11:24:21

pingIP地址

2010-07-01 10:20:41

SQL Server

2020-07-06 15:50:41

Python文件Linux

2010-03-12 16:30:27

Python文件

2010-03-17 14:18:27

Python open

2017-11-24 17:20:37

数据库数据仓库读写分离

2021-10-18 11:42:23

数据系统权衡
点赞
收藏

51CTO技术栈公众号