实验5
task6
with open('data6.csv','r',encoding='gbk') as f: data=f.read().split('\n') del data[0] for i in range(len(data)): data[i]=eval(data[i]) data[i]=float(data[i]) print('原始数据:') print(data) data1=[] for i in range(len(data)): data1.append(int(data[i]+0.5)) print('四舍五入后数据:') print(data1) for i in range(len(data)): data[i]=str(data[i]) data1[i]=str(data1[i]) title=['原始数据','四舍五入后数据'] info=[] for i in range(len(data)): info.append([data[i],data1[i]]) with open('data6_processed.csv','w',encoding='gbk') as f: f.write(','.join(title)+'\n') for i in info: f.write(','.join(i)+'\n')
task7
with open('data7.csv','r',encoding='gbk') as f: data1 = f.read().split('\n') del data1[0] lsta = [] lstm = [] for i in data1: lst1 = i.split(',') if lst1[2] == 'Acting': lsta.append(lst1) else:lstm.append(lst1) lstm.sort(key=lambda x:x[-1],reverse = True) lsta.sort(key=lambda x:x[-1],reverse = True) info = lsta + lstm title = ['学号','姓名','专业','分数'] with open('data7_processed.csv','w',encoding='gbk')as f: f.write(','.join(title)+'\n') for items in info: f.write(','.join(items)+'\n') print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3])) for i in info: print('{:<10}'.format(i[0]),'{:<10}'.format(i[1]),'{:<10}'.format(i[2]),'{:<15}'.format(i[3]))
task8
with open('hamlet.txt','r',encoding='utf-8') as f: data1=f.readlines() with open('hamlet.txt','r',encoding='utf-8') as f: data2=f.read() print('行数:',len(data1)) print('单词数:',len(data2.split())) alphanum=0 othernum=0 for i in data2: if i.isalpha(): alphanum +=1 else: othernum+=1 print('字符数',alphanum+othernum) 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)
task9
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("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))
task10
task10_1
with open('data10_stu.txt','r',encoding='utf-8') as f: data = f.readlines() print('{:*^40}'.format('抽点开始')) n = int(input('输入随机抽点人数:')) import random x = [] sum1 = 0 x2 = '' while n != 0: counts = 0 while counts < n: new = random.randint(0, len(data) - 1) if new in x: new = random.randint(0, len(data) - 1) else: counts +=1 x.append(new) for i in range(sum1,sum1+n): print(data[x[i]]) x2 += data[x[i]] sum1 += n n = int(input('输入随机抽点人数:')) with open('20230602.txt','w',encoding='utf-8') as f: f.writelines(x2) print('{:*^40}'.format('抽点结束'))
task10_2
with open('data10_stu.txt','r',encoding='utf-8') as f: data = f.readlines() print('{:*^40}'.format('抽点开始')) n = int(input('输入随机抽点人数:')) import random x = [] sum1 = 0 x2 = '' while n != 0: counts = 0 while counts < n: new = random.randint(0, len(data) - 1) if new in x: new = random.randint(0, len(data) - 1) else: counts +=1 x.append(new) for i in range(sum1,sum1+n): print(data[x[i]]) x2 += data[x[i]] sum1 += n n = int(input('输入随机抽点人数:')) with open('20230602.txt','w',encoding='utf-8') as f: f.writelines(x2) print('{:*^40}'.format('抽点结束'))