task6.py
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_after.csv','w',encoding='gbk',newline='') as f : 36 f_writer=csv.writer(f) 37 f_writer.writerow(title) 38 f_writer.writerows(data_new)
运行结果:
task7.py
1 with open('data7.csv','r',encoding='gbk') as f: 2 data1 = f.read().split('\n') 3 del data1[0] 4 lsta = [] 5 lstb = [] 6 for i in data1: 7 lst1 = i.split(',') 8 if lst1[2] == 'Acting': 9 lsta.append(lst1) 10 else: 11 lstb.append(lst1) 12 lstb.sort(key=lambda x:x[-1],reverse = True) 13 lsta.sort(key=lambda x:x[-1],reverse = True) 14 info = lsta + lstb 15 title = ['学号','姓名','专业','分数'] 16 with open('data7_processed.csv','w',encoding='gbk') as f: 17 f.write(','.join(title)+'\n') 18 for items in info: 19 f.write(','.join(items)+'\n') 20 print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3])) 21 for i in info: 22 print('{:<10}'.format(i[0]),'{:<10}'.format(i[1]),'{:<10}'.format(i[2]),'{:<15}'.format(i[3]))
运行结果:
task8.py
1 with open('hamlet.txt', 'r', encoding='utf-8') as f: 2 data = f.read() 3 4 print('hamlet.txt粗略统计:') 5 print(f'行数:{len(data.splitlines())}') 6 print(f'单词数:{len(data.split())}') 7 print(f'字符数:{len(data)}') 8 print(f'空格数:{data.count(" ")}') 9 10 data1 = data.splitlines() 11 with open('hamlet_with_line_number.txt', 'w', encoding='utf-8') as f: 12 for number, words in enumerate(data1, 1): 13 f.write(f'{number} {words}\n')
运行结果:
task9.py
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 print('{:<10} {:<10} {:<10} '.format('姓名','出生日期','年龄')) 14 import datetime 15 today=datetime.date.today() 16 valid_ls=[] 17 for ls in info: 18 if is_valid(ls[1]) is True: 19 y, m, d = ls[1][6:10], ls[1][10:12], ls[1][12:14] 20 birth = y + '-' + m + '-' + d 21 ls.append(birth) 22 del ls[1] 23 birthday=datetime.date(year=int(y),month=int(m),day=int(d)) 24 age=str(round((today-birthday).days/365)) 25 ls.append(age) 26 valid_ls.append(ls) 27 def takeThird(elem): 28 return elem[2] 29 valid_ls.sort(key=takeThird,reverse = True) 30 for ls in valid_ls: 31 for i in ls: 32 print('{:8}'.format(i),end=' ') 33 print()
运行结果:
task10.1.py
1 import datetime 2 t = datetime.datetime.now() 3 filename = t.strftime('%Y%m%d') + '.txt' 4 n=eval(input('输入随机抽取人数:')) 5 with open('data10_stu.txt', 'r', encoding = 'utf-8') as f: 6 data=f.readlines() 7 8 def random_selection(n): 9 import random 10 demo = random.sample(data, n) 11 return demo 12 x=random_selection(n) 13 for i in x : 14 print(i) 15 16 with open(filename,'w',encoding='utf-8')as f: 17 f.writelines(x)
运行结果:
task10.2.py
1 import random 2 import datetime 3 t = datetime.datetime.now() 4 title = t.strftime('%Y%m%d') 5 6 with open('data10_stu.txt', 'r', encoding='utf-8') as f: 7 data = f.readlines() 8 data0 = [i.strip('\n').split('\t') for i in data] 9 10 print(f'{"抽点开始":*^40}') 11 n = eval(input('输入随机抽点人数:')) 12 13 a = 0 14 while n != 0: 15 data1 = random.sample(data0, n) 16 a += 1 17 for i in data1: 18 print(' '.join(i)) 19 data0.remove(i) 20 if a == 1: 21 with open(f'{title}.txt', 'w', encoding='utf-8') as f: 22 for i in data1: 23 f.write(f'{i[0]}\t{i[1]:6s}\t{i[2]:10s}\n') 24 else: 25 with open(f'{title}.txt', 'a', encoding='utf-8') as f: 26 for i in data1: 27 f.write(f'{i[0]}\t{i[1]:6s}\t{i[2]:10s}\n') 28 n = eval(input('输入随机抽点人数:')) 29 print(f'{"抽点结束":*^40}')
运行结果:
标签:文件,encoding,data1,open,编程,实验,print,txt,data From: https://www.cnblogs.com/hyy052416/p/17458980.html