当前位置:网站首页>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')
边栏推荐
- NBA playoff match chart
- Jerry's plug-in 4m flash to view the processing method with a size of only 1m on the PC [chapter]
- [redis] publish and subscribe
- Share deadlock problems encountered in insert into select (project practice)
- 第019讲:函数:我的地盘听我的 | 课后测试题及答案
- 300. 最长递增子序列 ●●
- Differences between watch, computed and methods
- las 点云创建网格
- DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]
- 杰理之外挂 4M 的 flash 在 PC 上查看大小只有 1M 的处理方法【篇】
猜你喜欢
![Jerry's dynamic switching EQ document [chapter]](/img/2d/9a0245b87fb05ea61dbfc7922ea766.png)
Jerry's dynamic switching EQ document [chapter]

杰理之外挂 4M 的 flash 在 PC 上查看大小只有 1M 的处理方法【篇】

鸿蒙第三次培训

长安旗下阿维塔科技增资扩股落定:宁德时代将持股约24%!

第033讲:异常处理:你不可能总是对的2 | 课后测试题及答案

杰理之列免晶振一拖八烧录升级【篇】
![[redis] publish and subscribe](/img/50/0c2fbbb8f56fccdd3222b77efdd723.png)
[redis] publish and subscribe
![DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]](/img/8a/ce164a5538bd8edf10eba5e4e8abe6.png)
DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]

软考必备资料大放送,全科目软考资料都给你备好了!
![[redis]配置文件](/img/1c/05c06d59c9efb5983f877822db333c.png)
[redis]配置文件
随机推荐
las 点云创建网格
快速排序模板 & 注意事项
Learning websites that programmers must see
密码学系列之:PKI的证书格式表示X.509
第014-15讲:字符串 (见小甲鱼新版27讲-32讲)| 课后测试题及答案
IDC发布中国数据治理报告 亿信华辰第一
HarmonyOS应用开发培训第二次
杰理之外挂 4M 的 flash 在 PC 上查看大小只有 1M 的处理方法【篇】
Apple Objective-C source code
List of outstanding talents: no crystal vibration, one drag, eight burn and upgrade [chapter]
[redis] publish and subscribe
牛客 52次月赛 B牛牛的身高 (思维题 模拟题)
84- I am on Internet & lt; 52 SQL statement performance optimization strategies & gt; Some views of
[redis]Redis6的事务操作
杰理之在music模式下提示音使用打断模式无法播放的问题【篇】
[138. copy linked list with random pointer]
≥server2012R2系统,禁用系统自带的部分计划任务
Baijia forum Wu Zetian
View Apple product warranty status
分享insert into select遇到的死锁问题(项目实战)