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

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

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

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

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

拆分编组同样不难:

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

 

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

view plaincopy to clipboardprint?  
import pickle   
 
fileHandle = open ( 'pickleFile.txt', 'w' )   
testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson',
 [ 1, 2, 7 ] ]   
pickle.dump ( testList, fileHandle )    fileHandle.close()      import pickle     fileHandle = open ( 'pickleFile.txt', 'w' )   testList = [ 123, { 'Calories' : 190 }, 'Mr. Anderson', 

[ 1, 2, 7 ] ]  
pickle.dump ( testList, fileHandle )   fileHandle.close()view plaincopy to clipboardprint?   import pickle      fileHandle = open ( 'pickleFile.txt' )    testList = pickle.load ( fileHandle )    fileHandle.close()      import pickle     fileHandle = open ( 'pickleFile.txt' )   testList = pickle.load ( fileHandle )   fileHandle.close()     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

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

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

以上是对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系统

2010-07-09 17:07:14

SQL Server数

2021-08-05 10:00:02

Python编程语言

2010-03-05 09:40:08

Python递归

2019-11-19 11:20:25

Python数据结构Windows

2021-02-26 20:55:56

JavaNIO随机

2021-07-20 15:42:05

编程语言PythonJava

2010-07-12 08:36:35

SQL Server数

2009-07-15 16:42:03

iBATIS读写CLO

2022-11-07 07:04:25

2009-12-02 11:24:21

pingIP地址

2010-07-01 10:20:41

SQL Server

2010-03-12 16:30:27

Python文件

2010-03-17 14:18:27

Python open

2025-01-13 09:00:00

Python文件读写代码

2020-07-06 15:50:41

Python文件Linux

2021-10-18 11:42:23

数据系统权衡
点赞
收藏

51CTO技术栈公众号