实验5 文件应用编程
实验任务6:
task6.py
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_processed.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)
运行结果截图
实验任务7
task7.py
1 with open('data7.csv','r',encoding='gbk') as f: 2 data = f.read().split('\n') 3 4 del data[0] 5 lsta = [] 6 lstb = [] 7 for i in data: 8 lst1 = i.split(',') 9 if lst1[2] == 'Acting': 10 lsta.append(lst1) 11 else: 12 lstb.append(lst1) 13 14 lstb.sort(key=lambda x:x[-1],reverse = True) 15 lsta.sort(key=lambda x:x[-1],reverse = True) 16 info = lsta + lstb 17 title = ['学号','姓名','专业','分数'] 18 19 with open('data7_processed.csv','w',encoding='gbk') as f: 20 f.write(','.join(title)+'\n') 21 for items in info: 22 f.write(','.join(items)+'\n') 23 24 print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3])) 25 26 for i in info: 27 print('{:<10}'.format(i[0]),'{:<10}'.format(i[1]),'{:<10}'.format(i[2]),'{:<15}'.format(i[3]))
运行结果截图
实验任务8
task8.py
1 lines = 0 2 words = 0 3 sum1 = 0 4 space = 0 5 with open('hamlet.txt','r',encoding='utf-8') as f: 6 for line in f: 7 word = line.split() 8 lines += 1 9 words += len(word) 10 sum1 += len(line) 11 for i in line: 12 if i == ' ': 13 space += 1 14 else: 15 pass 16 print('hamlet.txt粗滤统计:') 17 print(f'行数:{lines}') 18 print(f'单词数:{words}') 19 print(f'字符数:{sum1}') 20 print(f'空格数:{space}') 21 22 with open('hamlet.txt','r',encoding = 'utf-8') as f: 23 text = f.readlines() 24 25 for i in range(len(text)): 26 text[i] = str(i+1) + ' ' + text[i] 27 28 with open('hamlet_with_line_number.txt','w',encoding = 'utf-8') as f: 29 f.writelines(text)
运行结果截图
实验任务9
task9.py
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: 7 return False 8 9 10 with open('data9_id.txt','r',encoding='utf-8') as f: 11 data = f.read().split('\n') 12 del data[0] 13 data2 = [] 14 data3 =[] 15 print('姓名,出生日期,年龄') 16 17 for i in data: 18 lst = i.split(',') 19 data2.append(lst) 20 lst2 = [] 21 22 for i in data2: 23 if is_valid(i[1]) == True: 24 name = i[0] 25 btd = i[1][6:14] 26 age = str(2023 - int(i[1][6:10])) 27 lst2.append([name,btd,age]) 28 lst2.sort(key=lambda x:x[2],reverse=True) 29 30 for i in lst2: 31 print(i[0],end=',') 32 print(f'{i[1][:4]}-{i[1][4:6]}-{i[1][6:8]}',end=',') 33 print(i[2])
运行结果截图
实验任务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 import random 6 x = [] 7 x2 = '' 8 counts = 0 9 while counts < n: 10 new = random.randint(0, len(data) - 1) 11 if new in x: 12 new = random.randint(0, len(data) - 1) 13 else: 14 counts +=1 15 x.append(new) 16 for i in range(n): 17 print(data[x[i]]) 18 x2 += data[x[i]] 19 20 with open('20230602.txt','w',encoding='utf-8') as f: 21 f.writelines(x2)
运行结果截图
task10_2.py
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('20230602.txt','w',encoding='utf-8') as f: 25 f.writelines(x2) 26 27 print('{:*^40}'.format('抽点结束'))
运行结果截图
标签:文件,old,encoding,open,编程,实验,print,new,data From: https://www.cnblogs.com/xycproduction/p/17445668.html