实验任务6
实验源码:
with open('C:/Users/Lenovo/Desktop/data6.csv','r',encoding='gbk')as f: data1 = f.read().split('\n') del data1[0] for i in range(len(data1)): data1[i] = eval(data1[i]) data1[i] = float(data1[i]) print('原始数据:') print(data1) data2 = [] for i in range(len(data1)): data2.append(int(data1[i]+0.5)) print('四舍五入后数据:') print(data2) for i in range(len(data1)): data1[i] = str(data1[i]) for i in range(len(data1)): data2[i] = str(data2[i]) title = ['原始数据','四舍五入后数据'] info = [] for i in range(len(data1)): info.append([data1[i],data2[i]]) with open('data6_processed.csv','w',encoding='utf-8') as f: f.write(','.join(title)+'\n') for item in info: f.write(','.join(item)+'\n')
测试截图:
实验任务7
实验源码:
with open("C:/Users/Lenovo/Desktop/data7.csv",'r',encoding='gbk')as f: title=[f.readline()] data=f.readlines() def getscore(ls): return ls[len(ls)-1] data=[line.strip('\n').split(',')for line in data] data.sort(key=lambda x:(x[2],-int(x[3]))) with open('data7_processed.csv', 'w', encoding = 'gbk') as f: f.write(','.join(title)) for item in data: f.write(','.join(item) + '\n') title=title[0].strip('\n').split(',') print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3])) for i in data: print('{:<10}'.format(i[0]), '{:<10}'.format(i[1]), '{:<10}'.format(i[2]), '{:<10}'.format(i[3]))
测试截图:
实验任务8
实验源码:
with open("C:/Users/Lenovo/Desktop/hamlet.txt",'r',encoding='utf-8')as f: data1=f.readlines() print('行数:',len(data1)) with open("C:/Users/Lenovo/Desktop/hamlet.txt",'r',encoding='utf-8')as f: data2=f.read() print('单词数:',len(data2.split())) isalpha=0 others=0 for i in data2: if i.isalpha(): isalpha+=1 else: others+=1 print('字符数:',isalpha+others) print('空格数:',data2.count(' ')) with open('halmet_with_line_number.txt','w',encoding='utf-8')as f: data3=data2.splitlines() data4=[] for i in range(len(data3)): data4.append(f'{i+1} '+data1[i]+'\n') f.writelines(data4)
测试截图:
实验任务9
实验源码:
def is_valid(id): if len(id)==18 and id[:-1].isdigit() and (id[-1].isdigit() or id[-1]=='X'): return True else: return False with open("C:/Users/Lenovo/Desktop/data9_id.txt",'r',encoding='utf-8')as f: data=f.readlines() data.remove(data[0]) data0=[i.strip('\n').split(',') for i in data] data1=[] for i in data0: if is_valid(i[1])==True: i[1]=i[1][6:14] data1.append(i) import datetime t=datetime.datetime.now() y=t.strftime('%Y%m%d') for i in data1: i.append(str(int(y)-int(i[1][0:8]))) data2=sorted(data1,key=lambda x:-int(x[2])) print('姓名, 出生日期, 年龄') for i in data2: i[1]=i[1][0:4]+'-'+i[1][4:6]+'-'+i[1][6:8] i[2]=i[2][0:2] print(', '.join(i))
测试截图:
实验任务10(1)
实验源码:
测试截图:
实验任务10(2)
实验源码:
print(f"{'抽点开始':*^40}") import random as r with open("C:/Users/Lenovo/Desktop/data10_stu.txt",'r',encoding = 'utf-8') as f: stu = f.readlines() def func(n): stu_list = [] while True: if n != 0: i = r.randint(0,len(stu)-1) if stu[i] not in stu_list: stu_list.append(stu[i]) print(stu[i]) n -= 1 else: pass else: print(f"{'抽点结束':*^40}") break n = int(input('输入随机抽点人数:')) func(n)
测试截图:
实验任务10(2)
实验源码:
print(f"{'抽点开始':*^40}") import random as r with open("C:/Users/Lenovo/Desktop/data10_stu.txt",'r',encoding = 'utf-8') as f: stu = f.readlines() def func(n): stu_list = [] while True: if n != 0: i = r.randint(0,len(stu)-1) if stu[i] not in stu_list: stu_list.append(stu[i]) print(stu[i]) n -= 1 else: pass if n==0: break while True: n = int(input('输入随机抽点人数:')) if n != 0: func(n) else: print(f"{'抽点结束':*^40}") break
测试截图:
标签:encoding,len,stu,实验,print,data1,data2 From: https://www.cnblogs.com/qwertasd123/p/17457708.html