欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用pickle存儲數(shù)據(jù)dump 和 load實例講解

 更新時間:2019年12月30日 17:51:58   作者:mygodhome  
今天小編就為大家分享一篇使用pickle存儲數(shù)據(jù)dump 和 load實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

使用pickle模塊來dump你的數(shù)據(jù):對上篇博客里的sketch.txt文件:

import os
import sys
import pickle
 
man=[ ]
other=[ ]
try:
    data=open('sketch.txt')
    for each_line in data:
        try:
            (role,line_spoken)=each_line.split(':',1)
            line_spoken=line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()
except IOError:
    nester.print_lol('The data file is missing!')
 
try:
    with open('man_data.txt','wb') as man_file:
      pickle.dump(man,man_file)
    with open('other_data.txt','wb') as other_file:
      pickle.dump(other,other_file)
    
 
 
except IOError as err:
  print('File error: ' + str(err))
except pickle.PickleError as perr:
  print('Pickling error: ' + str(perr))
 

打開man_data.txt,看結(jié)果:

€]q (X'  Is this the right room for an argument?qX  No you haven't!qX  When?qX  No you didn't!qX  You didn't!qX  You did not!qX=  Ah! (taking out his wallet and paying) Just the five minutes.qX  You most certainly did not!qX  Oh no you didn't!q X  Oh no you didn't!q
X  Oh look, this isn't an argument!qX  No it isn't!qX  It's just contradiction!q
X  It IS!qX  You just contradicted me!qX  You DID!qX  You did just then!qX"  (exasperated) Oh, this is futile!!qX
  Yes it is!qe.

把已存儲在man_data.txt上的二進制文件,恢復(fù)成可以讀的文件,存放在new_man.txt中:

import nester
import os
import sys
import pickle
 
man=[ ]
other=[ ]
new_man=[ ]
 
try:
    data=open('sketch.txt')
    for each_line in data:
        try:
            (role,line_spoken)=each_line.split(':',1)
            line_spoken=line_spoken.strip()
            if role == 'Man':
                man.append(line_spoken)
            elif role == 'Other Man':
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()
except IOError:
    print_lol('The data file is missing!')
 
try:
#    with open('man_data.txt','wb') as man_file:
#      pickle.dump(man,man_file)
#    with open('other_data.txt','wb') as other_file:
#      pickle.dump(other,other_file)
 
  with open('man_data.txt','rb') as man_file:
    new_man=pickle.load(man_file)
 
except IOError as err:
  print('File error: ' + str(err))
except pickle.PickleError as perr:
  print('Pickling error: ' + str(perr))

查看結(jié)果:

 RESTART: C:/Users/ThinkPad/AppData/Local/Programs/Python/Python36-32/chapter4-134-pickle.py 
>>> import nester
>>> nester.print_lol(new_man)
Is this the right room for an argument?
No you haven't!
When?
No you didn't!
You didn't!
You did not!
Ah! (taking out his wallet and paying) Just the five minutes.
You most certainly did not!
Oh no you didn't!
Oh no you didn't!
Oh look, this isn't an argument!
No it isn't!
It's just contradiction!
It IS!
You just contradicted me!
You DID!
You did just then!
(exasperated) Oh, this is futile!!
Yes it is!
>>> import os
>>> os.getcwd()
'C:\\Users\\ThinkPad\\AppData\\Local\\Programs\\Python\\Python36-32'
>>>

若是想保存new_man.txt到磁盤文件,可以加:

  with open('new_man.txt','w') as new_man_file:
    nester.print_lol(new_man,fn=new_man_file)

以上這篇使用pickle存儲數(shù)據(jù)dump 和 load實例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 初學(xué)Python函數(shù)的筆記整理

    初學(xué)Python函數(shù)的筆記整理

    這篇文章主要介紹了初學(xué)Python函數(shù)的整理筆記,包括一些基礎(chǔ)的參數(shù)使用方法以及匿名函數(shù)等特性的使用,需要的朋友可以參考下
    2015-04-04
  • Python進階語法之類的繼承

    Python進階語法之類的繼承

    這篇文章主要為大家介紹了Python類的繼承,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • 三步實現(xiàn)Django Paginator分頁的方法

    三步實現(xiàn)Django Paginator分頁的方法

    這篇文章主要介紹了三步實現(xiàn)Django Paginator分頁的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 利用Python字符畫生成甜心教主

    利用Python字符畫生成甜心教主

    字符畫是一系列字符的組合,我們可以把字符看作是比較大塊的像素,一個字符能表現(xiàn)一種顏色,字符的種類越多,可以表現(xiàn)的顏色也越多,圖片也會更有層次感。?本文將利用Python字符畫繪制一個甜心教主王心凌,需要的可以參考一下
    2022-05-05
  • python使用列表的最佳方案

    python使用列表的最佳方案

    這篇文章主要介紹了python使用列表的最佳方式,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-08-08
  • 詳解python 中in 的 用法

    詳解python 中in 的 用法

    in在Python中是操作符,具體來說是成員操作符。這篇文章主要介紹了python 中in 的 用法,需要的朋友可以參考下
    2019-12-12
  • Ubuntu安裝Jupyter Notebook教程

    Ubuntu安裝Jupyter Notebook教程

    這篇文章主要為大家詳細介紹了Ubuntu安裝Jupyter Notebook教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • python @propert裝飾器使用方法原理解析

    python @propert裝飾器使用方法原理解析

    這篇文章主要介紹了python @propert裝飾器使用方法原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • 在python的WEB框架Flask中使用多個配置文件的解決方法

    在python的WEB框架Flask中使用多個配置文件的解決方法

    所謂配置文件管理,就是在不改變源代碼的前提下,擁有兩套(甚至多套)配置文件,分別用于開發(fā)環(huán)境和生產(chǎn)環(huán)境,通過命令行選擇配置文件
    2014-04-04
  • 基于pytorch 預(yù)訓(xùn)練的詞向量用法詳解

    基于pytorch 預(yù)訓(xùn)練的詞向量用法詳解

    今天小編就為大家分享一篇基于pytorch 預(yù)訓(xùn)練的詞向量用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01

最新評論