自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python數(shù)據(jù)編組對(duì)文字串的讀寫(xiě)

開(kāi)發(fā) 后端
Python數(shù)據(jù)編組是一種在國(guó)際上應(yīng)用也相當(dāng)廣泛的計(jì)算機(jī)語(yǔ)言,下面的文章就是對(duì)其相關(guān)實(shí)際應(yīng)用方案的具體介紹,忘、希望對(duì)你會(huì)有所幫助。

如果你對(duì)Python數(shù)據(jù)編組這種計(jì)算機(jī)語(yǔ)言有不解之處時(shí),或想了解Python數(shù)據(jù)編組的實(shí)際相關(guān)應(yīng)用方案時(shí),你可以瀏覽我們的文章,希望我們的文章就是對(duì)你會(huì)有所收獲。以下是文章的具體介紹。

使用前一節(jié)中介紹的模塊,可以實(shí)現(xiàn)在文件中對(duì)字符串的讀寫(xiě)。然而,有的時(shí)候,需要傳遞其它類(lèi)型的數(shù)據(jù)。如list、tuple、dictionary和其它對(duì)象。在Python數(shù)據(jù)編組中,你可以使用Pickling來(lái)完成。你可以使用Python標(biāo)準(zhǔn)庫(kù)中的“pickle”模塊完成數(shù)據(jù)編組。下面,我們來(lái)編組一個(gè)包含字符串和數(shù)字的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.  

 

現(xiàn)在Python數(shù)據(jù)編組試試存儲(chǔ)更加復(fù)雜的數(shù)據(jù):

  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數(shù)據(jù)編組的“pickle”模塊編組確實(shí)很簡(jiǎn)單。眾多對(duì)象可以通過(guò)它來(lái)存儲(chǔ)到文件中。如果可以的話(huà),“cPickle”同樣勝任這個(gè)工作。它和“pickle”模塊一樣,但是速度更快:

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

以上是對(duì)Python數(shù)據(jù)編組實(shí)際應(yīng)用的相關(guān)內(nèi)容的部分介紹。
 

責(zé)任編輯:佚名 來(lái)源: linux.chinaitlab.com
相關(guān)推薦

2017-08-01 17:34:47

Linux內(nèi)核驅(qū)動(dòng)文件讀寫(xiě)

2010-03-12 15:49:46

Python字串查找

2020-01-09 10:47:15

HDFS數(shù)據(jù)文件

2010-04-30 11:22:23

Unix系統(tǒng)

2010-07-09 17:07:14

SQL Server數(shù)

2021-08-05 10:00:02

Python編程語(yǔ)言

2010-03-05 09:40:08

Python遞歸

2019-11-19 11:20:25

Python數(shù)據(jù)結(jié)構(gòu)Windows

2021-02-26 20:55:56

JavaNIO隨機(jī)

2010-07-12 08:36:35

SQL Server數(shù)

2021-07-20 15:42:05

編程語(yǔ)言PythonJava

2009-07-15 16:42:03

iBATIS讀寫(xiě)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文件讀寫(xiě)代碼

2020-07-06 15:50:41

Python文件Linux

2021-10-18 11:42:23

數(shù)據(jù)系統(tǒng)權(quán)衡
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)