当前位置:网站首页>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')
边栏推荐
猜你喜欢
随机推荐
86- to attend & lt; SQL writing and rewriting training & gt; 's participants add a second-hand case
When the AUX1 or aux2 channel is used in Jerry's aux mode, the program will reset the problem [chapter]
Install MySQL in ECS (version 2022)
ByteDance proposes a lightweight and efficient new network mocovit, which has better performance than GhostNet and mobilenetv3 in CV tasks such as classification and detection
TC397 Flash
杰理之硬件上 DACL 输出,DAC 输出左右声道的声音【篇】
Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver
[redis] three new data types
localStorage、sessionStorage 和 cookie 的区别大总结
2022 group programming TIANTI race L1
基于AI驱动大分子药物发现,「华深智药」获近5亿元A轮融资
杰理之动态切换 EQ 文件【篇】
NBA playoff match chart
第014-15讲:字符串 (见小甲鱼新版27讲-32讲)| 课后测试题及答案
Baijia forum in the 13th year of Yongzheng (lower part)
[redis]Redis6的主从复制
[20. valid brackets]
CVPR2022 | 海德堡大学《深度视觉相似性与度量学习》教程
74- how to remedy the loss of Oracle to MySQL for this kind of SQL optimization?
Baijia forum Huangdi Neijing (Part I)





![[redis]redis6 transaction operations](/img/50/639867a2fcb92082ea262a8a19bb68.png)

![[160. cross linked list]](/img/79/177e2c86bd80d12f42b3edfa1974ec.png)
