当前位置:网站首页>Lesson 031: permanent storage: pickle a jar of delicious pickles | after class test questions and answers

Lesson 031: permanent storage: pickle a jar of delicious pickles | after class test questions and answers

2022-06-22 21:36:00 ChaseTimLee

Test questions :

0. pickle What is the essence of ?

answer :pickle The essence of is to use some algorithms to transform your data objects “ pickled ” Binary file , Stored on disk , Of course, it can also be placed in the database or transmitted to another computer through the network .

1. Use pickle How to store data ?

answer :pickle.dump(data, file) # The first parameter is the data object to be stored , The second parameter is the target stored file object , Be careful to use... First ’wb’ The pattern of open Documents _

2. Use pickle How to read data ?

answer :pickle.load(file) # The parameter is the target stored file object , Be careful to use... First ’rb’ The pattern of open Documents _

3. Use pickle Can I save as ”*.txt” Files of type ?

answer : Sure , But it is garbled after opening , Because it is written in binary mode .( For example, please refer to the following section 0 topic )

use one's hands

0. Write a program , This time it is required to use pickle Will file ( record.txt (1.1 KB, amount of downloads : 11455) )

 Small customer service : Little turtle , Today, a client asked if you have a girlfriend ?
 Little turtle : Why ??
 Small customer service : I told her you had a girlfriend !
 Little turtle :......
 Small customer service : She asked you to think about her after you broke up ! And I said :" You want to buy a USB drive , Let me help you keep an eye on ~"
 Little turtle : so what ?
 Small customer service : She bought two , It's good to send a shipment ~
 Little turtle : Uh ...... you're really something !
 Small customer service : That is , Who makes me a fish C The cutest customer service ~
 Little turtle : Next time someone wants to flirt with you, I won't stop ~
 Small customer service : roll !!!
================================================================================
 Small customer service : Little turtle , It's funny to have a good comment .
 Little turtle : Oh ?
 Small customer service :" With the little turtle , My mother won't have to worry about my study any more ~"
 Little turtle : Ha ha ha , I see ya , I also posted a microblog ~
 Small customer service : Mm-hmm , I read your microblog ~
 Little turtle : Yossi ~
 Small customer service : There is a reply “ Holding a small turtle in his left hand , Holding a lighter in his right hand , Where not where ,so easy ^_^”
 Little turtle :T_T
================================================================================
 Small customer service : Little turtle , A member wants to see you today 
 Little turtle : Oh ? What's up? ?
 Small customer service : He said that your monthly salary for a student has exceeded 12k 了 !!
 Little turtle : Where? ?
 Small customer service : Shanghai 
 Little turtle : That's normal , Which company ?
 Small customer service : He didn't say .
 Little turtle : Oh 
 Small customer service : The eldest brother , Why is my salary so low ?? It's time for a raise !!
 Little turtle : ah , What did you say? ? I'm outside , It's so noisy here ......
 Small customer service : roll !!!

The conversations in the are pickled into different documents according to the following requirements ( you 're right , It's No 29 The content of the lecture has been slightly changed , Can you write it yourself ?):
The dialogue of the little turtle is saved as boy_.txt The file of ( Get rid of “ Little turtle :”)
The conversation of small customer service is saved separately as girl_
.txt The file of ( Get rid of “ Small customer service :”)
There are three conversations in the file , Save as boy_1.txt, girl_1.txt,boy_2.txt, girl_2.txt, boy_3.txt, gril_3.txt common 6 File ( Tips : Different conversations in the file have been used “==========” Division )

import pickle

def save_file(boy, girl, count):
    file_name_boy = 'boy_' + str(count) + '.txt'
    file_name_girl = 'girl_' + str(count) + '.txt'

    boy_file = open(file_name_boy, 'wb') #  Remember to add it  b  Acridine 
    girl_file = open(file_name_girl, 'wb') #  Remember to add it  b  Acridine 

    pickle.dump(boy, boy_file)
    pickle.dump(girl, girl_file)

    boy_file.close()
    girl_file.close()

def split_file(file_name):
    count = 1
    boy = []
    girl = []

    f = open(file_name)

    for each_line in f:
        if each_line[:6] != '======':
            (role, line_spoken) = each_line.split(':', 1)
            if role == ' Little turtle ':
                boy.append(line_spoken)
            if role == ' Small customer service ':
                girl.append(line_spoken)
        else:
            save_file(boy, girl, count)

            boy = []
            girl = []
            count += 1 

    save_file(boy, girl, count)
    f.close()

split_file('record.txt')

原网站

版权声明
本文为[ChaseTimLee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222005193864.html