6.实验任务6
task6.py
实验代码:
with open('data6.csv','r',encoding='gbk') as f: data1 = f.read().split('\n') del data1[0] print('原始数据:') print(data1) for i in range(len(data1)): data1[i] = float(data1[i]) data2 = [] for i in range(len(data1)): data2.append(int(data1[i]+0.5)) print('四舍五入后数据:') print(data2) title = ['原始数据','四舍五入后数据'] info = [] for i in range(len(data1)): info.append([str(data1[i]),str(data2[i])]) with open('data6_processed.csv','w',encoding='gbk') as f: f.write(','.join(title)+'\n') for item in info: f.write(','.join(item)+'\n')
实验结果截图:
实验文件截图:
7.实验任务7
task7.py
实验代码:
with open('data7.csv','r',encoding='gbk') as f: data1 = f.read().split('\n') del data1[0] lsta = [] lstm = [] for i in data1: data1.sort(key = lambda i :(i[-2],i[-1]),reverse = True) for i in data1: lst1 = i.split(',') if lst1[2] == 'Acting': lsta.append(lst1) else: lstm.append(lst1) info = lsta+lstm title = ['学号','姓名','专业','分数'] with open('data7_processed.csv','w',encoding='gbk') as f: f.write(','.join(title)+'\n') for items in info: f.write(','.join(items)+'\n') print('{:<10}'.format(title[0]),'{:<10}'.format(title[1]),'{:<10}'.format(title[2]),'{:<15}'.format(title[3])) for i in info: print('{:<10}'.format(i[0]),'{:<10}'.format(i[1]),'{:<10}'.format(i[2]),'{:<15}'.format(i[3]))
实验结果截图:
实验文件截图:
8.实验任务8
task8.py
实验代码:
with open('hamlet.txt','r') as f: data = f.read() print('hamlet.txt粗略统计:') data1 = data.splitlines() print(f'行数:{len(data1)}') print(f'单词数:{len(data.split())}') print(f'字符数:{len(data)}') k = data.count(' ') print(f'空格数:{k}') data2=[] for i in range(len(data1)): data2.append(f'{i+1} '+ data1[i] + '\n') with open('hamlet_with_line_number.txt','w') as f: f.writelines(data2)
实验结果截图:
实验文件截图:
9.实验任务9
task9.py
实验代码:
import datetime def is_valid(x): if len(x) == 18 and x[:-1].isdigit() and (x[-1].isdigit() or x[-1] == 'X'): return True else: return False with open('data9_id.txt', 'r', encoding='utf-8') as f: data0 = f.readlines() title = data0.pop(0) data0 = [i.strip('\n').split(',') for i in data0] data1 = [] for i in data0: if is_valid(i[1]): data1.append(i) print(data1) t = datetime.datetime.now() y = t.strftime('%Y%m%d') for i in data1: age = str(int(y)-int(i[1][6:14])) i.append(age[:2]) i[1] = i[1][6:10] + '-' + i[1][10:12] + '-' + i[1][12:14] data2 = sorted(data1, key=lambda x: -int(x[2])) print('姓名, 出生日期, 年龄') for i in data2: print(', '.join(i))
实验结果截图:
10.实验任务10
task10_1.py
实验代码:
import random import datetime with open('data10_stu.txt', 'r', encoding='utf-8') as f: data = f.readlines() data0 = [i.strip('\n').split('\t') for i in data] n = eval(input('输入随机抽点人数:')) data1 = random.sample(data0, n) for i in data1: print(' '.join(i)) t = datetime.datetime.now() title = t.strftime('%Y%m%d') with open(f'{title}.txt', 'w', encoding='utf-8') as f: for i in data1: f.write(f'{i[0]}\t{i[1]:6s}\t{i[2]:10s}\n')
实验结果截图:
实验文件截图:
task10_2.py
实验代码:
import random import datetime t = datetime.datetime.now() title = t.strftime('%Y%m%d') with open('data10_stu.txt', 'r', encoding='utf-8') as f: data = f.readlines() data0 = [i.strip('\n').split('\t') for i in data] print(f'{"抽点开始":*^40}') n = eval(input('输入随机抽点人数:')) a = 0 while n != 0: data1 = random.sample(data0, n) a += 1 for i in data1: print(' '.join(i)) data0.remove(i) if a == 1: with open(f'{title}.txt', 'w', encoding='utf-8') as f: for i in data1: f.write(f'{i[0]}\t{i[1]:6s}\t{i[2]:10s}\n') else: with open(f'{title}.txt', 'a', encoding='utf-8') as f: for i in data1: f.write(f'{i[0]}\t{i[1]:6s}\t{i[2]:10s}\n') n = eval(input('输入随机抽点人数:')) print(f'{"抽点结束":*^40}')
实验结果截图:
实验文件截图:
标签:截图,title,open,实验,print,data1 From: https://www.cnblogs.com/xjw88/p/17461518.html