实验五 文件应用编程
实验任务六
程序源代码:
1 with open('data6.csv', 'r', encoding='gbk') as f: 2 old_data = f.read().split('\n') 3 del old_data[0] 4 processed_data = [] 5 for i in range(len(old_data)): 6 if eval(old_data[i]) + 0.5 >= int(eval(old_data[i])) + 1: 7 processed_data.append(str(int(eval(old_data[i])) + 1)) 8 else: 9 processed_data.append(str(int(eval(old_data[i])))) 10 title = ['原始数据', '四舍五入后数据'] 11 new = [] 12 for i in range(len(old_data)): 13 new.append([old_data[i], processed_data[i]]) 14 with open('data6.csv','w',encoding = 'gbk') as f: 15 f.write(','.join(title)+'\n') 16 for i in new: 17 f.write(','.join(i) + '\n') 18 print('原始数据:') 19 print(old_data) 20 print(f'四舍五入后数据:') 21 print(new)
实验结果:
实验任务七
程序源代码:
1 with open('data7.csv','r',encoding = 'gbk') as f: 2 data = f.readlines() 3 data_t = ['学号','姓名','专业','分数'] 4 data1 = [i.strip('\n').split(',') for i in data] 5 data1.remove(data1[0]) 6 7 data2=sorted(data1,key=lambda x:(x[2],-int(x[3]))) 8 with open('data7_processed.csv','w',encoding = 'gbk') as f: 9 f.write(','.join(data_t) + '\n') 10 for item in data2: 11 f.write(','.join(item) + '\n') 12 13 print(f'{"学号":10s}{"姓名":10s}{"专业":10s}{"分数":10s}') 14 for a,b,c,d in data2: 15 print('{:<10s}{:<10s}{:<10s}{:<10}'.format(a,b,c,d))
实验结果:
实验任务八
程序源代码:
1 with open('hamlet.txt','r',encoding='utf-8')as f: 2 data = f.read() 3 data1 = f.readlines() 4 5 print('hamlet.txt粗略统计:') 6 print(f'行数:{len(data.splitlines())}') 7 print(f'单词数:{len(data.split())}') 8 print(f'字符数:{len(data)}') 9 print(f'空格数:{data.count(" ")}') 10 data2 = [] 11 for i in range(len(data1)): 12 data2.append('{}'.format(i+1)+'.'+data1[i]+'\n') 13 with open('hamlet.txt','w',encoding='utf-8') as f: 14 for i in data2: 15 f.write(','.join(data2)+'\n')
实验结果:
实验任务九
程序源代码:
1 def is_valid(sfz): 2 if len(sfz) != 18: 3 return False 4 elif (sfz[:-1].isnumeric() and sfz[-1] == 'X') or sfz.isnumeric(): 5 return True 6 else:return False 7 8 9 with open('data9_id.txt','r',encoding='utf-8') as f: 10 data = f.read().split('\n') 11 del data[0] 12 data2 = [] 13 data3 =[] 14 print('姓名,出生日期,年龄') 15 for i in data: 16 lst = i.split(',') 17 data2.append(lst) 18 lst2 = [] 19 for i in data2: 20 if is_valid(i[1]) == True: 21 name = i[0] 22 btd = i[1][6:14] 23 age = str(2023 - int(i[1][6:10])) 24 lst2.append([name,btd,age]) 25 lst2.sort(key=lambda x:x[2],reverse=True) 26 for i in lst2: 27 print(i[0],end=',') 28 print(f'{i[1][:4]}-{i[1][4:6]}-{i[1][6:8]}',end=',') 29 print(i[2])
实验结果:
实验任务十
task10-1
程序源代码:
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 9 def random_selection(n): 10 import random 11 demo = random.sample(data, n) 12 return demo 13 x=random_selection(n) 14 for i in x : 15 print(i)
实验结果:
task10-2
程序源代码:
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 sum1 = 0 8 x2 = '' 9 while n != 0: 10 counts = 0 11 while counts < n: 12 new = random.randint(0, len(data) - 1) 13 if new in x: 14 new = random.randint(0, len(data) - 1) 15 else: 16 counts += 1 17 x.append(new) 18 for i in range(sum1, sum1+n): 19 print(data[x[i]]) 20 x2 += data[x[i]] 21 sum1 += n 22 n = int(input('输入随机抽点人数:')) 23 24 with open('20230601.txt', 'w', encoding='utf-8') as f: 25 f.writelines(x2) 26 27 print('{:*^40}'.format('抽点结束'))
实验结果:
标签:文件,old,encoding,open,编程,实验,print,data,data2 From: https://www.cnblogs.com/pansufei/p/17445709.html