当前位置:网站首页>Lesson 029: Documents: a task? After class test questions and answers
Lesson 029: Documents: a task? After class test questions and answers
2022-06-22 21:35:00 【ChaseTimLee】
use one's hands :
0. Write a program , Accept user input and save it as a new file , The program implementation is shown in the figure :

def file_write(file_name):
f = open(file_name, 'w')
print(' Please enter the content 【 Separate input \':w\' Save and exit 】:')
while True:
write_some = input()
if write_some != ':w':
f.write('%s\n' % write_some)
else:
break
f.close()
file_name = input(' Please enter filename :')
file_write(file_name)
1. Write a program , Compare two files entered by the user , If different , Show all the different line numbers and the position of the first different character , The program implementation is shown in the figure :

def file_compare(file1, file2):
f1 = open(file1)
f2 = open(file2)
count = 0 # The statistical number of rows
differ = [] # Count different numbers
for line1 in f1:
line2 = f2.readline()
count += 1
if line1 != line2:
differ.append(count)
f1.close()
f2.close()
return differ
file1 = input(' Please enter the first file name to compare :')
file2 = input(' Please enter another file name to compare :')
differ = file_compare(file1, file2)
if len(differ) == 0:
print(' The two files are exactly the same !')
else:
print(' The two files share 【%d】 Different :' % len(differ))
for each in differ:
print(' The first %d It's different ' % each)
2. Write a program , When the user enters the file name and the number of lines (N) after , Put the front of the file N The line content is printed to the screen , The program implementation is shown in the figure :

def file_view(file_name, line_num):
print('\n file %s Before %s Is as follows :\n' % (file_name, line_num))
f = open(file_name)
for i in range(int(line_num)):
print(f.readline(), end= '')
f.close()
file_name = input(r' Please enter the file to open (C:\\test.txt):')
line_num = input(' Please enter the first few lines of the file to be displayed :')
file_view(file_name, line_num)
3. Uh , It has to be said that our users are becoming more and more tricky . It is required to expand on the previous question , The user can input the number of lines to be displayed at will .( Such as the input 13:21 Print page 13 Go to the first place 21 That's ok , Input :21 Before printing 21 That's ok , Input 21: Then print from 21 Everything from the beginning of the line to the end of the file )

def file_view(file_name, line_num):
if line_num.strip() == ':':
begin = '1'
end = '-1'
(begin, end) = line_num.split(':')
if begin == '':
begin = '1'
if end == '':
end = '-1'
if begin == '1' and end == '-1':
prompt = ' Full text '
elif begin == '1':
prompt = ' From the beginning to %s' % end
elif end == '-1':
prompt = ' from %s To end ' % begin
else:
prompt = ' From %s Go to the first place %s That's ok ' % (begin, end)
print('\n file %s%s Is as follows :\n' % (file_name, prompt))
begin = int(begin) - 1
end = int(end)
lines = end - begin
f = open(file_name)
for i in range(begin): # To consume begin Previous content
f.readline()
if lines < 0:
print(f.read())
else:
for j in range(lines):
print(f.readline(), end='')
f.close()
file_name = input(r' Please enter the file to open (C:\\test.txt):')
line_num = input(' Please enter the number of rows to display 【 The format is as follows 13:21 or :21 or 21: or : 】:')
file_view(file_name, line_num)
4. Write a program , Realization “ All replacement ” function , The program implementation is shown in the figure :

def file_replace(file_name, rep_word, new_word):
f_read = open(file_name)
content = []
count = 0
for eachline in f_read:
if rep_word in eachline:
count = eachline.count(rep_word) #count I think I should use this
eachline = eachline.replace(rep_word, new_word)
content.append(eachline)
decide = input('\n file %s The Communist Party of China has %s individual 【%s】\n Are you sure you want to put all the 【%s】 Replace with 【%s】 Do you ?\n【YES/NO】:' \
% (file_name, count, rep_word, rep_word, new_word))
if decide in ['YES', 'Yes', 'yes']:
f_write = open(file_name, 'w')
f_write.writelines(content)
f_write.close()
f_read.close()
file_name = input(' Please enter filename :')
rep_word = input(' Please enter the word or character to be replaced :')
new_word = input(' Please enter a new word or character :')
file_replace(file_name, rep_word, new_word)
边栏推荐
- PHP image making
- LeetCode#20.有效的括号
- 513. 找树左下角的值 / 剑指 Offer II 091. 粉刷房子
- 浅析 Open API 设计规范
- 杰理之动态切换 EQ 文件【篇】
- RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemmStridedBatched( ha
- 2022 chemical automation control instrument examination exercises and online simulation examination
- Share deadlock problems encountered in insert into select (project practice)
- How to operate redis on the IntelliJ idea database console
- PlainSelect. getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
猜你喜欢
![[513. find the value in the lower left corner of the tree]](/img/6d/b2ec8e3072a65c20c586941e6b2a85.png)
[513. find the value in the lower left corner of the tree]

300. 最长递增子序列 ●●

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

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

Share deadlock problems encountered in insert into select (project practice)

Install MySQL in ECS (version 2022)
![[redis]redis6 master-slave replication](/img/47/3be33a0d7435bd75cdd6e7b4ea51d4.png)
[redis]redis6 master-slave replication

Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model

分享insert into select遇到的死锁问题(项目实战)
![Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver](/img/29/8dd188cc4e909562862b5f2c57c898.png)
Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver
随机推荐
92 match for several_ Recognize SQL write example
万字长文 | 使用 RBAC 限制对 Kubernetes 资源的访问
【ICML2022】利用虚拟节点促进图结构学习
杰理之开启四声道通话近端卡顿问题【篇】
2022危险化学品经营单位主要负责人上岗证题库及模拟考试
查询es分页下标超过1万
Baijia forum Daqin rise (lower part)
数据库总结:mysql在开发过程中常见的问题及优化
2022年A特种设备相关管理(电梯)考题及模拟考试
Differences between watch, computed and methods
[206. reverse linked list]
[142. circular linked list II]
513. 找树左下角的值 / 剑指 Offer II 091. 粉刷房子
redis学习笔记
[redis]三种新数据类型
[513. find the value in the lower left corner of the tree]
密码学系列之:PKI的证书格式表示X.509
2022 question bank and simulated examination for work license of main principals of hazardous chemical business units
ACM. HJ45 名字的漂亮度 ●●
When the AUX1 or aux2 channel is used in Jerry's aux mode, the program will reset the problem [chapter]