• [Python]小甲鱼Python视频第031课(pickle)课后题及解答


    # -*- coding: utf-8 -*-
    """
    Created on Fri Feb 15 23:05:24 2019
    
    @author: fengs
    """
    
    """
    0. pickle的实质是什么?
        a.将对象数据以二进制形式保存在文件中
        b.将对象数据以二进制形式从文件中读取
      输入输出二进制流 1. 使用pickle的什么方法存储数据? dump() 2. 使用pickle的什么方法读取数据? load() 3. 使用pickle能不能保存为”*.txt”类型的文件? 可以,后缀名可以任意指定,只不过用文本编辑器打开时,是乱码 """ """ 动动手: 编写一个程序,这次要求使用pickle将文件( record.txt (1.1 KB, 下载次数: 3988) )里的对话按照以下要求腌制成不同文件(没错,是第29讲的内容小改,考考你自己能写出来吗?): 小甲鱼的对话单独保存为boy_*.txt的文件(去掉“小甲鱼:”) 小客服的对话单独保存为girl_*.txt的文件(去掉“小客服:”) 文件中总共有三段对话,分别保存为boy_1.txt, girl_1.txt,boy_2.txt, girl_2.txt, boy_3.txt, gril_3.txt共6个文件(提示:文件中不同的对话间已经使用“==========”分割) """ import pickle file_name_base_boy = 'boy_'; file_name_base_girl = 'girl_'; file_count = 1; #读取record文件 f_record = open('record.txt','r'); file_name_boy = file_name_base_boy + str(file_count) + ".pkl"; file_name_girl = file_name_base_girl + str(file_count) + ".pkl"; fid_boy = open(file_name_boy,'wb'); fid_girl = open(file_name_girl,'wb'); boy_spoken = []; girl_spoken = []; for each_line in f_record: print(each_line); if each_line[:6]!='======': [name,spoken]= each_line.split(':',1); if name == '小甲鱼': boy_spoken += spoken; if name == '小客服': girl_spoken += spoken; else: pickle.dump(boy_spoken,fid_boy); pickle.dump(girl_spoken,fid_girl); fid_boy.close(); fid_girl.close(); boy_spoken = []; girl_spoken = []; file_count +=1; file_name_boy = file_name_base_boy + str(file_count) + ".pkl"; file_name_girl = file_name_base_girl + str(file_count) + ".pkl"; fid_boy = open(file_name_boy,'wb'); fid_girl = open(file_name_girl,'wb'); else: pickle.dump(boy_spoken,fid_boy); pickle.dump(girl_spoken,fid_girl); fid_boy.close(); fid_girl.close(); #验证代码 def read_pkl(): file_name_boy1 = 'boy_1.pkl'; file_name_boy2 = 'boy_2.pkl'; file_name_boy3 = 'boy_3.pkl'; file_name_girl1 = 'girl_1.pkl'; file_name_girl2 = 'girl_2.pkl'; file_name_girl3 = 'girl_3.pkl'; fid_boy1 = open(file_name_boy1,'rb'); fid_boy2 = open(file_name_boy2,'rb'); fid_boy3 = open(file_name_boy3,'rb'); fid_girl1 = open(file_name_girl1,'rb'); fid_girl2 = open(file_name_girl2,'rb'); fid_girl3 = open(file_name_girl3,'rb'); boy_spoken_1 = pickle.load(fid_boy1); boy_spoken_2 = pickle.load(fid_boy2); boy_spoken_3 = pickle.load(fid_boy3); girl_spoken_1 = pickle.load(fid_girl1); girl_spoken_2 = pickle.load(fid_girl2); girl_spoken_3 = pickle.load(fid_girl3); print('-------'); print(str(boy_spoken_1)); print('-------'); print(str(boy_spoken_2)); print('-------'); print(str(boy_spoken_3)); print('-------'); print(str(girl_spoken_1)); print('-------'); print(str(girl_spoken_2)); print('-------'); print(str(girl_spoken_3)); print('-------'); fid_boy1.close(); fid_boy2.close(); fid_boy3.close(); fid_girl1.close(); fid_girl2.close(); fid_girl3.close(); read_pkl();

      

    ~不再更新,都不让我写公式,博客园太拉胯了
  • 相关阅读:
    std 迭代器的几种用法
    Cocos2dx3.1+xcode +lua配置
    php获取文件创建时间、修改时间
    如何创建一个基于jquery的编辑器
    封装ajax,简单的模仿jquery提交
    如何实现SQL事务的提交,又不对外进行污染
    .net mvc结合微软提供的FormsAuthenticationTicket登陆
    Http GET、Post方式的请求总结
    post 报文请求接口方法
    zip格式压缩、解压缩(C#)
  • 原文地址:https://www.cnblogs.com/alimy/p/10386526.html
Copyright © 2020-2023  润新知