task6
源代码:
with open ('data6.csv','r',encoding='gbk') as f: data=f.readlines() data.remove('原始数据\n') data1=[] for i in data: if i[-1]=='\n': i=i[:-1] data1.append(float(i)) print('原始数据:') print(data1) data2=[round(i+0.2) for i in data1] print('四舍五入后数据:') print(data2) with open('data6_processed.csv','w',encoding='gbk') as f: f.write('原始数据,四舍五入后数据\n') for i in range(len(data2)): f.write(str(data1[i])+','+str(data2[i])+'\n')
运行结果截图:
task7
源代码:
with open('data7.csv','r',encoding='gbk') as f: data=f.readlines() data1=[i.strip('\n').split(',') for i in data] data1.remove(data1[0]) data_acting=[] data_music=[] for i in data1: if i[2]=='Acting': data_acting.append(i) elif i[2]=='Music': data_music.append(i) data2=sorted(data_acting,key=lambda x:x[3],reverse=True) data3=sorted(data_music,key=lambda x:x[3],reverse=True) data4=data2+data3 with open('data7_processed.csv','w',encoding='gbk') as f: f.write('学号,姓名,专业,分数\n') for i in range(len(data4)): f.write(','.join(data4[i])+'\n')
运行结果截图
task8
源代码
with open('hamlet.txt','r',encoding='utf-8')as f: data=f.readlines() print('行数:',len(data)) x='' for line in data: x+=line alpha=0 b=0 for i in x: if i.isalpha(): alpha+=1 else: b+=1 word=len(x.split()) print('字符数:',alpha+b) print('单词数:',word) print('空格数:',x.count(' ')) data = [line.strip('\n').split(',') for line in data] with open('hamlet_with_line_number','w',encoding='utf-8') as f: for i,j in enumerate(data,start=1): j=j[0] f.write("{}{}{}".format(i,' ',j)+'\n')
运行结果截图
task9
源代码
def is_valid(id): if len(list(id)) != 18: return False elif str(id).isnumeric() or (list(id)[-1]=='X' and str(id)[:-1].isnumeric()): 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:x[2],reverse=True) 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
源代码
#10-1 import datetime t = datetime.datetime.now() filename = t.strftime('%Y%m%d') + '.txt' n=eval(input('输入随机抽取人数:')) with open('data10_stu.txt', 'r', encoding = 'utf-8') as f: data=f.readlines() def random_selection(n): import random demo = random.sample(data, n) return demo x=random_selection(n) for i in x : print(i)
运行结果截图
源代码
#10-2 import random import datetime print('*'*20,'抽点开始','*'*20) with open('data10_stu.txt','r',encoding='UTF-8')as f: data=f.readlines() data1=[i.strip('\n').split('\t') for i in data] t=datetime.datetime.now() d=t.strftime('%Y%m%d') n=int(input('输入随机抽点人数:')) x=0 while n!=0: data2=random.sample(data1,n) for i in data2: print(' '.join(i)) data1.remove(i) if x==1: with open('{}.txt'.format(d),'w',encoding='UTF-8') as f: for i in data2: i[0] += '\t' i[1] += '\t' i[2] += '\n' f.writelines(i) else: with open('{}.txt'.format(d), 'a', encoding='UTF-8') as f: for i in data2: i[0] += '\t' i[1] += '\t' i[2] += '\n' f.writelines(i) n = int(input('输入随机抽点人数:')) print('*'*20,'抽点结束','*'*20)
运行结果截图
标签:open,encoding,data1,实验,print,data,data2 From: https://www.cnblogs.com/wybpyx/p/17461483.html