实验任务6
task6
实验源码
1 with open('data6.csv','r',encoding='gbk') as f: 2 data1 = f.read().split('\n') 3 del data1[0] 4 print('原始数据:') 5 print(data1) 6 7 for i in range(len(data1)): 8 data1[i] = float(data1[i]) 9 data2 = [] 10 for i in range(len(data1)): 11 data2.append(int(data1[i]+0.5)) 12 print('四舍五入后数据:') 13 print(data2) 14 15 title = ['原始数据','四舍五入后数据'] 16 info = [] 17 for i in range(len(data1)): 18 info.append([str(data1[i]),str(data2[i])]) 19 with open('data6_processed.csv','w',encoding='gbk') as f: 20 f.write(','.join(title)+'\n') 21 for item in info: 22 f.write(','.join(item)+'\n')
运行测试截图
实验任务7
task7
实验源码
1 with open('data7.csv','r',encoding='gbk') as f: 2 data1 = f.read().split('\n') 3 del data1[0] 4 5 lsta = [] 6 lstm = [] 7 for i in data1: 8 data1.sort(key = lambda i :(i[-2],i[-1]),reverse = True) 9 for i in data1: 10 lst1 = i.split(',') 11 if lst1[2] == 'Acting': 12 lsta.append(lst1) 13 else: 14 lstm.append(lst1) 15 16 info = lsta+lstm 17 title = ['学号','姓名','专业','分数'] 18 with open('data7_processed.csv','w',encoding='gbk') as f: 19 f.write(','.join(title)+'\n') 20 for items in info: 21 f.write(','.join(items)+'\n') 22 print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3])) 23 for i in info: 24 print('{:<10}'.format(i[0]),'{:<10}'.format(i[1]),'{:<10}'.format(i[2]),'{:<15}'.format(i[3]))
运行测试截图
实验任务8
实验源码
1 with open('hamlet.txt','r') 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 data2=[] 12 for i in range(len(data1)): 13 data2.append(f'{i+1} '+data1[i]+'\n') 14 15 with open(r'C:\Users\28678\Downloads\实验5数据文件\hamlet_with_line_number.txt', 16 'w') as f: 17 f.writelines(data2)
运行测试截图
实验任务9
实验源码
1 def is_valid(a): 2 if len(a) != 18: 3 return False 4 elif (a[:-1].isnumeric() and a[-1] == 'X') or (a.isnumeric()): 5 return True 6 else: 7 return False 8 9 with open('data9_id.txt','r') as f: 10 data = f.read().split('\n') 11 del data[0] 12 data1 = [] 13 print('姓名,出生日期,年龄') 14 for i in data: 15 lst = i.split(',') 16 data1.append(lst) 17 lst1 = [] 18 import datetime 19 t = datetime.datetime.now().year 20 for i in data1: 21 if is_valid(i[1]) == True: 22 name = i[0] 23 btd = i[1][6:14] 24 age = str(int(t)-int(i[1][6:10])) 25 lst1.append([name,btd,age]) 26 lst1.sort(key=lambda x:x[2],reverse=True) 27 for i in lst1: 28 print(i[0],end=',') 29 print(f'{i[1][:4]}-{i[1][4:6]}-{i[1][6:8]}',end=',') 30 print(i[2])
运行测试截图
实验任务10
task10.1
实验源码
1 with open('data10_stu.txt','r',encoding='utf-8') as f: 2 data = f.readlines() 3 print('{:*^40}'.format('抽点开始')) 4 n = int(input('输入随机抽点人数:')) 5 import random 6 x = [] 7 while len(set(x)) < n: 8 x.append(random.randint(0, len(data)-1)) 9 10 lst = [] 11 for i in x: 12 print(data[i],end = '\n') 13 lst.append(data[i]) 14 15 import datetime 16 t = datetime.datetime.now() 17 y = t.strftime('%Y%m%d') 18 with open(f'{y}.txt','w',encoding='utf-8') as f: 19 f.writelines(lst)
运行测试截图
task10.2
实验源码
1 with open('data10_stu.txt','r',encoding='utf-8') as f: 2 data = f.readlines() 3 print('{:*^40}'.format('抽点开始')) 4 lst1 = [] 5 x = [] 6 while True: 7 n = int(input('输入随机抽点人数:')) 8 if n == 0: 9 print('{:*^40}'.format('抽点结束')) 10 break 11 else: 12 x = [] 13 import random 14 while len(x) < n: 15 t = random.randint(0, len(data)-1) 16 if (str(t) not in lst1) and (str(t) not in x): 17 x.append(t) 18 lst1.append(t) 19 20 lst = [] 21 for i in x: 22 print(data[i],end = '\n') 23 lst.append(data[i]) 24 25 import datetime 26 t = datetime.datetime.now() 27 y = t.strftime('%Y%m%d') 28 with open(f'{y}.txt','w',encoding='utf-8') as f: 29 f.writelines(lst)
运行测试截图
标签:文件,lst1,data,编程,len,实验,print,data1,append From: https://www.cnblogs.com/siyi5981/p/17461596.html