实验任务6
源代码
1 with open('data6.csv','r',encoding='gbk')as f: 2 data=list(f.readlines()) 3 data=[i.strip('\n') for i in data ] 4 5 import decimal 6 decimal.getcontext().rounding='ROUND_HALF_UP' 7 title=[data[0],'四舍五入后的数据'] 8 data_new=[] 9 data_o=[] 10 for i in data: 11 if not i.isalpha() is True: 12 data_o.append(eval(i)) 13 print('原始数据:') 14 print(data_o) 15 for i in data: 16 if not i.isalpha() is True: 17 data_new.append([i]) 18 19 data_=[] 20 for i in data: 21 if not i.isalpha() is True: 22 index=data.index(i)-1 23 i=eval(i) 24 if i-int(i)>=0.5: 25 i=int(i)+1 26 data_new[index].append(str(i)) 27 data_.append(i) 28 else: 29 i=int(i) 30 data_new[index].append(str(i)) 31 data_.append(i) 32 print('四舍五入后的数据:') 33 print(data_) 34 import csv 35 with open('data6_processed.csv','w',encoding='gbk',newline='') as f : 36 f_writer=csv.writer(f) 37 f_writer.writerow(title) 38 f_writer.writerows(data_new)
运行截图
实验任务7
源代码
1 with open('data7.csv','r',encoding='gbk')as f: 2 title=[f.readline()] 3 data=f.readlines() 4 def getscore(ls): 5 return ls[len(ls)-1] 6 7 data=[line.strip('\n').split(',')for line in data] 8 9 data.sort(key=lambda x:(x[2],-int(x[3]))) 10 11 12 with open('data7_processed.csv', 'w', encoding = 'gbk') as f: 13 f.write(','.join(title)) 14 for item in data: 15 f.write(','.join(item) + '\n') 16 title=title[0].strip('\n').split(',') 17 18 print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3])) 19 for i in data: 20 print('{:<10}'.format(i[0]), '{:<10}'.format(i[1]), '{:<10}'.format(i[2]), '{:<10}'.format(i[3]))
运行截图
实验任务8
源代码
with open('hamlet.txt','r',encoding='gbk')as f: data=f.readlines() x='' for line in data: x+=line print('行数:',len(data)) print('字符数:',len(x)) print('单词数:',len(x.split())) print('空格数:',x.count(' '))for i in range(len(data)): if data[i]: data[i]=str(i+1)+' '+data[i] with open('hamlet_with_line_number.txt','w',encoding='gbk',newline='') as f : f.writelines(data)
实验任务9
源代码
1 with open('data9_id.txt','r',encoding='utf-8')as f: 2 data=f.readlines() 3 4 info=[line.strip('\n').split(',')for line in data] 5 del info[0] 6 7 def is_valid(id): 8 y=id[:17] 9 if (i in ['1','2','3','4','5','6','7','8','9','0','X'] for i in id) and len(id)==18 and y.isdigit() and (id[17]=='X' or id[17].isdigit()): 10 return True 11 else: 12 return False 13 14 print('姓名'+' '+'出生日期'+' '+'年龄') 15 import datetime 16 today=datetime.date.today() 17 valid_ls=[] 18 for ls in info: 19 if is_valid(ls[1]) is True: 20 y, m, d = ls[1][6:10], ls[1][10:12], ls[1][12:14] 21 birth = y + '-' + m + '-' + d 22 ls.append(birth) 23 del ls[1] 24 birthday=datetime.date(year=int(y),month=int(m),day=int(d)) 25 age=str(round((today-birthday).days/365)) 26 ls.append(age) 27 valid_ls.append(ls) 28 def takeThird(elem): 29 return elem[2] 30 valid_ls.sort(key=takeThird,reverse = True) 31 for ls in valid_ls: 32 for i in ls: 33 print('{:2}'.format(i),end=' ') 34 print()
运行截图
实验任务10
task10_1.py
源代码
1 with open('data10_stu.txt','r',encoding='utf-8') as f: 2 data=f.readlines() 3 4 n=int(input('输入随机抽取人数:')) 5 def random_selection(n): 6 import random 7 demo=random.sample(data,n) 8 return demo 9 x=random_selection(n) 10 for i in x: 11 print(i) 12 13 import datetime 14 t=datetime.datetime.now() 15 filename=t.strftime('%Y%m%d')+'.txt' 16 17 with open(filename,'w',encoding='utf-8') as f: 18 f.writelines(x)
运行截图
tast10_2
源代码
1 import datetime 2 t = datetime.datetime.now() 3 filename = t.strftime('%Y%m%d') + '.txt' 4 5 print('抽取开始'.center(50,'*')) 6 with open('data10_stu.txt', 'r', encoding='utf-8') as f: 7 data = f.readlines() 8 9 def random_selection(n): 10 import random 11 demo = random.sample(data, n) 12 return demo 13 selection=set() 14 numbers=[] 15 16 while True: 17 n = eval(input('输入随机抽点人数:')) 18 if n != 0: 19 x=random_selection(n) 20 for i in x: 21 print(i) 22 data.remove(i) 23 with open(filename,'a',encoding='utf-8') as f : 24 f.writelines(i) 25 else: 26 print('抽取结束'.center(50,'*')) 27 break
运行截图
标签:文件,encoding,open,编程,datetime,实验,ls,print,data From: https://www.cnblogs.com/zyj-/p/17454425.html