当前位置:网站首页>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')
边栏推荐
猜你喜欢

2022 question bank and simulated examination for work license of main principals of hazardous chemical business units

(DUC/DDC)数字上混频/正交下混频原理及matlab仿真
![kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动](/img/29/8dd188cc4e909562862b5f2c57c898.png)
kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动
![[redis] profile](/img/1c/05c06d59c9efb5983f877822db333c.png)
[redis] profile
![[160. cross linked list]](/img/79/177e2c86bd80d12f42b3edfa1974ec.png)
[160. cross linked list]

Arcgis中las点云数据抽稀

分享insert into select遇到的死锁问题(项目实战)

为了不曾忘却的纪念:孙剑专题
![[redis] publish and subscribe](/img/50/0c2fbbb8f56fccdd3222b77efdd723.png)
[redis] publish and subscribe

2022 chemical automation control instrument examination exercises and online simulation examination
随机推荐
Differences between watch, computed and methods
75- when left join encounters subquery
86- to attend & lt; SQL writing and rewriting training & gt; 's participants add a second-hand case
LeetCode#20. Valid parentheses
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
第014-15讲:字符串 (见小甲鱼新版27讲-32讲)| 课后测试题及答案
300. 最长递增子序列 ●●
万字长文 | 使用 RBAC 限制对 Kubernetes 资源的访问
Can the characteristics of different network structures be compared? Ant & meituan & NTU & Ali proposed a cross architecture self supervised video representation learning method CaCl, performance SOTA
Correspondence between int and char in C language
[513. find the value in the lower left corner of the tree]
快速排序模板 & 注意事项
2022 a special equipment related management (elevator) examination questions and simulation examination
杰理之动态切换 EQ 文件【篇】
80- paging query, not only writing
73- find the SQL example during the business peak period (report development class)
Jericho uses DP and DM for IO key detection [chapter]
ACM. HJ45 名字的漂亮度 ●●
71- analysis of an Oracle DBA interview with Alibaba in 2010
Jerry's dynamic switching EQ document [chapter]