目录
1.从键盘输入一些字符,逐个把它们写到指定的文件,直到输入一个@为止。
示例1: 请输入文件名: out.txt 请输入字符串: Python is open.@ 执行代码后,out.txt文件中内容为: Python is open.
程序代码:
print("输入@后的内容将不再输入到文件中")
try:
word = input("请输入您想输入到文件的字符:")
file_write = open("out.txt", "w",encoding="utf-8")
except Exception as reason:
print(reason)
else:
for file_word in word:
if file_word == "@":
break
else:
file_write.write(file_word)
file_write.close()
with open("out.txt", "r",encoding="utf-8") as file_read:
print(file_read.read())
file_read.close()
运行结果:
C:\ProgramData\Anaconda3\python.exe D:/blue7/01.py
输入@后的内容将不再输入到文件中
请输入您想输入到文件的字符:Python is open.@
Python is open.
进程已结束,退出代码0
2.老王的血压有些高,医生让家属给老王测血压。老王的女儿记录了一段时间的血压测量值,在文件xueyajilu.txt中,内容示例如下:
-
2020/7/2 6:00,140,82,136,90,69
-
2020/7/2 15:28,154,88,155,85,63
-
2020/7/3 6:30,131,82,139,74,61
-
2020/7/3 16:49,145,84,139,85,73
-
2020/7/4 5:03,152,87,131,85,63;
文件内各部分含义如下: 测量时间,左臂高压,左臂低压,右臂高压,右臂低压,心率 根据题意,实现下述功能。
(1)使用字典和列表类型进行数据分析,获取老王的左臂和右臂血压情况的对比表,输出到屏幕上,请注意每列对齐:
-
低压最高值
-
左臂和右臂的血压平均值
-
左臂和右臂的高压差平均值、低压差平均值
-
心率的平均值
程序代码1:
with open("xueyajilu.txt","w",encoding="gbk") as xueyajilu:
xueyajilu.write(
"2020/7/2 6:00 140,82,136,90,69""\n"
"2020/7/2 15:28 154,88,155,85,63""\n"
"2020/7/3 6:30 131,82,139,74,61""\n"
"2020/7/3 16:49 145,84,139,85,73""\n"
"2020/7/4 5:03 152,87,131,85,63""\n")
with open("xueyajilu.txt","r",encoding="gbk") as xueyajilu_read:
jilu = []
for xueyajilu_dictionary_word in xueyajilu_read.readlines():
xueyajilu_end = xueyajilu_dictionary_word.rstrip("\n")
jilu.append(xueyajilu_end)
xueya_dictionary = {}
count_end = 0
for j in jilu:
count_end += 1
xueya_dictionary[count_end] = [j]
diya_maxs = []
for diya in range(1,6):
diya_max = []
diya_max = xueya_dictionary[diya]
diya_maxs += diya_max
diya_count = []
for count in range(0,5):
diya_count.append(diya_maxs[count][19:21])
diya_count.append(diya_maxs[count][26:28])
print("低压最高值为:",max(diya_count))
for count in range(0,5):
diya_count.append(diya_maxs[count][15:18])
diya_count.append(diya_maxs[count][22:25])
diya_count_list_int = sum(list(map(int,diya_count)))
print("左臂和右臂的血压平均值为:","%.2f"%(diya_count_list_int/len(diya_count)))
gaoyaa_count = []
gaoyaa_count_fu = []
for gaoya_count in range(0,5):
gaoyaa_count.append(diya_maxs[gaoya_count][15:18])
gaoyaa_count_fu.append(diya_maxs[gaoya_count][22:25])
gaoyaa_count_int_list = sum(list(map(int,gaoyaa_count)))
gaoyaa_count_fu_int_list =sum(list(map(int,gaoyaa_count_fu)))
print("左右臂高压差平均值:",(gaoyaa_count_int_list-gaoyaa_count_fu_int_list)/5)
diyaya_left = []
diyaya_right = []
for diyaya_count in range(0,5):
diyaya_left.append(diya_maxs[diyaya_count][19:21])
diyaya_right.append(diya_maxs[diyaya_count][26:28])
diyaya_left_list_int = sum(list(map(int,diyaya_left)))
diyaya_right_list_int = sum(list(map(int,diyaya_right)))
print("左右臂低压差平均值:",(diyaya_left_list_int-diyaya_right_list_int)/len(diyaya_right))
xinlv = []
for xinlv_count in range(0,5):
xinlv.append(diya_maxs[xinlv_count][29:31])
xinlv_int_lis = sum(list(map(int,xinlv)))
print("心率平均值:",xinlv_int_lis/len(xinlv))
运行结果1:
C:\ProgramData\Anaconda3\python.exe D:/blue7/02.py
低压最高值为: 90
左臂和右臂的血压平均值为: 113.20
左右臂高压差平均值: 4.4
左右臂低压差平均值: 0.8
心率平均值: 65.8
进程已结束,退出代码0
程序代码2:
print("对比表"" ""左臂"" ""右臂")
left = []
right = []
with open("xueyajilu.txt","w",encoding="gbk") as xueyajilu:
xueyajilu.write(
"2020/7/2 6:00 140,82,136,90,69""\n"
"2020/7/2 15:28 154,88,155,85,63""\n"
"2020/7/3 6:30 131,82,139,74,61""\n"
"2020/7/3 16:49 145,84,139,85,73""\n"
"2020/7/4 5:03 152,87,131,85,63""\n")
with open("xueyajilu.txt","r",encoding="gbk") as xueyajilu_read:
jilu = []
for xueyajilu_dictionary_word in xueyajilu_read.readlines():
xueyajilu_end = xueyajilu_dictionary_word.rstrip("\n")
jilu.append(xueyajilu_end)
xueya_dictionary = {}
count_end = 0
for j in jilu:
count_end += 1
xueya_dictionary[count_end] = [j]
diya_maxs = []
for diya in range(1,6):
diya_max = []
diya_max = xueya_dictionary[diya]
diya_maxs += diya_max
diya_count_left = []
diya_count_right = []
for count in range(0,5):
diya_count_left.append(diya_maxs[count][19:21])
diya_count_right.append(diya_maxs[count][26:28])
print("低压最高值 ",max(diya_count_left)," ",max(diya_count_right))
right.append((max(diya_count_right)))
left.append((max(diya_count_left)))
diya_count_left_int_list = sum(list(map(int,diya_count_left)))
diya_count_right_int_list = sum(list(map(int,diya_count_right)))
print("低压平均值 ",diya_count_left_int_list/len(diya_count_left),
" ",diya_count_right_int_list/len(diya_count_right))
left.append("%.1f"%(diya_count_left_int_list/len(diya_count_left)))
right.append("%.1f"%(diya_count_right_int_list/len(diya_count_right)))
xueya_average_left = []
xueya_average_right = []
for average_count in range(0,5):
xueya_average_left.append(diya_maxs[average_count][15:18])
xueya_average_left.append(diya_maxs[average_count][19:21])
xueya_average_right.append(diya_maxs[average_count][22:25])
xueya_average_right.append(diya_maxs[average_count][26:28])
xueya_average_left_int_list = sum(list(map(int, xueya_average_left)))
xueya_average_right_int_list = sum(list(map(int,xueya_average_right)))
print("血压平均值 ",xueya_average_left_int_list/len(xueya_average_left),
" ",xueya_average_right_int_list/len(xueya_average_right))
left.append("%.1f"%(xueya_average_left_int_list/len(xueya_average_left)))
right.append("%.1f"%(xueya_average_right_int_list/len(xueya_average_right)))
xueya_left = []
xueya_right = []
for xueya_count in range(0,5):
xueya_left.append(diya_maxs[xueya_count][15:18])
xueya_right.append(diya_maxs[xueya_count][22:25])
xueya_left_int_list = sum(list(map(int,xueya_left)))
xueya_right_int_list = sum(list(map(int,xueya_right)))
print("高压平均值 ",xueya_left_int_list/len(xueya_left),
" ",xueya_right_int_list/len(xueya_left))
print("压差平均值 ","%.1f"%(xueya_left_int_list/len(xueya_left)-diya_count_left_int_list/len(diya_count_left)),
" ",xueya_right_int_list/len(xueya_left)-diya_count_right_int_list/len(diya_count_right))
left.append("%.1f"%(xueya_left_int_list/len(xueya_left)))
left.append("%.1f"%(xueya_left_int_list/len(xueya_left)-diya_count_left_int_list/len(diya_count_left)))
right.append("%.1f"%(xueya_right_int_list/len(xueya_left)))
right.append("%.1f"%(xueya_right_int_list/len(xueya_left)-diya_count_right_int_list/len(diya_count_right)))
xinlv = []
for xinlv_count in range(0,5):
xinlv.append(diya_maxs[xinlv_count][29:31])
xinlv_int_lis = sum(list(map(int,xinlv)))
print("心率平均值:",xinlv_int_lis/len(xinlv))
运行结果2:
C:\ProgramData\Anaconda3\python.exe D:/blue7/021.py
对比表 左臂 右臂
低压最高值 88 90
低压平均值 84.6 83.8
血压平均值 114.5 111.9
高压平均值 144.4 140.0
压差平均值 59.8 56.2
心率平均值: 65.8
进程已结束,退出代码0
(2)上述显示的五个项目,如果左臂有大于50%的项目高于右臂,则输出“结论:左臂血压偏高”;如果等于50%的项目高于右臂,则输出“结论:左臂血压与右臂血压相当”;如果小于50%的项目高于右臂,则输出“结论:右臂血压偏高”。
程序代码:
print("对比表"" ""左臂"" ""右臂")
left = []
right = []
with open("xueyajilu.txt","w",encoding="gbk") as xueyajilu:
xueyajilu.write(
"2020/7/2 6:00 140,82,136,90,69""\n"
"2020/7/2 15:28 154,88,155,85,63""\n"
"2020/7/3 6:30 131,82,139,74,61""\n"
"2020/7/3 16:49 145,84,139,85,73""\n"
"2020/7/4 5:03 152,87,131,85,63""\n")
with open("xueyajilu.txt","r",encoding="gbk") as xueyajilu_read:
jilu = []
for xueyajilu_dictionary_word in xueyajilu_read.readlines():
xueyajilu_end = xueyajilu_dictionary_word.rstrip("\n")
jilu.append(xueyajilu_end)
xueya_dictionary = {}
count_end = 0
for j in jilu:
count_end += 1
xueya_dictionary[count_end] = [j]
diya_maxs = []
for diya in range(1,6):
diya_max = []
diya_max = xueya_dictionary[diya]
diya_maxs += diya_max
diya_count_left = []
diya_count_right = []
for count in range(0,5):
diya_count_left.append(diya_maxs[count][19:21])
diya_count_right.append(diya_maxs[count][26:28])
print("低压最高值 ",max(diya_count_left)," ",max(diya_count_right))
right.append((max(diya_count_right)))
left.append((max(diya_count_left)))
diya_count_left_int_list = sum(list(map(int,diya_count_left)))
diya_count_right_int_list = sum(list(map(int,diya_count_right)))
print("低压平均值 ",diya_count_left_int_list/len(diya_count_left),
" ",diya_count_right_int_list/len(diya_count_right))
left.append("%.1f"%(diya_count_left_int_list/len(diya_count_left)))
right.append("%.1f"%(diya_count_right_int_list/len(diya_count_right)))
xueya_average_left = []
xueya_average_right = []
for average_count in range(0,5):
xueya_average_left.append(diya_maxs[average_count][15:18])
xueya_average_left.append(diya_maxs[average_count][19:21])
xueya_average_right.append(diya_maxs[average_count][22:25])
xueya_average_right.append(diya_maxs[average_count][26:28])
xueya_average_left_int_list = sum(list(map(int, xueya_average_left)))
xueya_average_right_int_list = sum(list(map(int,xueya_average_right)))
print("血压平均值 ",xueya_average_left_int_list/len(xueya_average_left),
" ",xueya_average_right_int_list/len(xueya_average_right))
left.append("%.1f"%(xueya_average_left_int_list/len(xueya_average_left)))
right.append("%.1f"%(xueya_average_right_int_list/len(xueya_average_right)))
xueya_left = []
xueya_right = []
for xueya_count in range(0,5):
xueya_left.append(diya_maxs[xueya_count][15:18])
xueya_right.append(diya_maxs[xueya_count][22:25])
xueya_left_int_list = sum(list(map(int,xueya_left)))
xueya_right_int_list = sum(list(map(int,xueya_right)))
print("高压平均值 ",xueya_left_int_list/len(xueya_left),
" ",xueya_right_int_list/len(xueya_left))
print("压差平均值 ","%.1f"%(xueya_left_int_list/len(xueya_left)-diya_count_left_int_list/len(diya_count_left)),
" ",xueya_right_int_list/len(xueya_left)-diya_count_right_int_list/len(diya_count_right))
left.append("%.1f"%(xueya_left_int_list/len(xueya_left)))
left.append("%.1f"%(xueya_left_int_list/len(xueya_left)-diya_count_left_int_list/len(diya_count_left)))
right.append("%.1f"%(xueya_right_int_list/len(xueya_left)))
right.append("%.1f"%(xueya_right_int_list/len(xueya_left)-diya_count_right_int_list/len(diya_count_right)))
xinlv = []
for xinlv_count in range(0,5):
xinlv.append(diya_maxs[xinlv_count][29:31])
xinlv_int_lis = sum(list(map(int,xinlv)))
print("心率平均值:",xinlv_int_lis/len(xinlv))
count = 0
count1 = 0
for i in range(0,5):
if left[i]>right[i]:
count += 1
elif left[i]<right[i]:
count1 += 1
if count > count1:
print("左臂血压偏高")
elif count1 > count:
print("右臂血压偏高")
elif count == count1:
print("左右臂血压相当")
运行结果:
C:\ProgramData\Anaconda3\python.exe D:/blue7/021.py
对比表 左臂 右臂
低压最高值 88 90
低压平均值 84.6 83.8
血压平均值 114.5 111.9
高压平均值 144.4 140.0
压差平均值 59.8 56.2
心率平均值: 65.8
左臂血压偏高
进程已结束,退出代码0
3.假设当前目录下有一个文件名为class_score.txt的文本文件,存放着某班学生的学号(第1列)、语文成绩(第2列)和数学成绩(第3列),以空格分割各列数据。请编写程序完成下列要求:
(1)分别求出这个班语文成绩和数学成绩的平均分(保留1位小数)并输出。
(2)找出这个班两门课都不及格(<60)的学生,输出这些学生的学号、语文成绩和数学成绩。
(3)找出这个班两门课的平均成绩为优秀(≥90分)的学生,输出这些学生的学号、语文成绩和数学成绩和平均成绩。 程序运行效果如图所示。
程序代码:
with open("class_score.txt","w",encoding= "utf-8") as open_file:
open_file.write("20212501122 90 90""\n"
"20212501123 56 58""\n"
"20212501124 54 44""\n"
"20212501125 59 84""\n")
open_file.close()
with open("class_score.txt","r",encoding="utf-8") as read_file:
jilu = []
jilu_math = []
jilu_chinese = []
for message in read_file:
message_end = message.rstrip("\n")
jilu.append(message_end)
for j in range(0,4):
jilu_chinese.append(jilu[j][12:14])
jilu_math.append(jilu[j][14:17])
jilu_chinese_int_list = sum(list(map(int,jilu_chinese)))
jilu_math_int_list = sum(list(map(int, jilu_math)))
print("语文平均成绩为:","%.1f"%(jilu_chinese_int_list/len(jilu)))
print("数学平均成绩为:", "%.1f"%(jilu_math_int_list/len(jilu)))
for i in range(0,3):
if jilu[i][12:14] < "60" and jilu[i][14:17] < "60":
print("两门成绩都不及格的学生:")
print(jilu[i])
if int(jilu[i][12:14])+int(jilu[i][14:17])/2 >= 90:
print("平均成绩为优秀的学生:")
print(jilu[i],int(jilu[i][12:14])+int(jilu[i][14:17])/2)
运行结果:
C:\ProgramData\Anaconda3\python.exe D:/blue7/03.py
语文平均成绩为: 64.8
数学平均成绩为: 69.0
平均成绩为优秀的学生:
20212501122 90 90 135.0
两门成绩都不及格的学生:
20212501123 56 58
两门成绩都不及格的学生:
20212501124 54 44
进程已结束,退出代码0
4.编写程序,打开任意的文本文件,在指定的位置产生一个相同文件的副本,即实现文件的拷贝功能。
程序代码:
import shutil
import re
import os
try:
path_old = input("请输入您要复制的文本路径:")
os.path.exists(path_old)
if re.findall(".txt|doc|docx|xlsx|pptx|htm|html|PDF",path_old):
print("文件有效!")
else:
raise ValueError("文件格式错误!")
except ValueError as reason:
print(reason)
else:
shutil.copy(path_old,path_old+"-(副本)")
运行结果:
C:\ProgramData\Anaconda3\python.exe D:/blue7/04.py
请输入您要复制的文本路径:D:\蓝桥云.txt
文件有效!
进程已结束,退出代码0
5.在自己电脑任一盘符中新建以OS_Test命名的文件夹,并在该文件夹中新建以.doc .bmp .txt .png .jpeg .xlsx为扩展名的文件若干,请写一个程序,删除掉OS_Test目录里面(不包含子目录)所有的扩展名为.txt的文件,并将删除掉的文件名称打印出来。
程序代码:
from os.path import isdir,join,splitext
from os import remove,listdir
filetypes = ".txt"
def delfile(directory):
if not isdir(directory):
return
for filename in listdir(directory):
name,ext = splitext(filename)
if ext == filetypes:
print(filename)
temp = join(directory,filename)
if isdir(temp):
delfile(temp)
remove(temp)
delfile("D:\OS_Test")
运行结果:
C:\ProgramData\Anaconda3\python.exe D:/blue7/05.py
蓝桥云.txt
进程已结束,退出代码0
文章出处登录后可见!
已经登录?立即刷新