3.实验任务3
1 with open('data3.txt', 'r', encoding='utf-8') as f1: 2 data3 = f1.read().split('\n') 3 a = data3.pop(0) 4 data1 = [eval(d) for d in data3] 5 b = '四舍五入后数据' 6 data2 = [round(i) for i in data1] 7 print(f'{a}:\n{data1}') 8 print(f'{b}:\n{data2}') 9 data1.insert(0, a) 10 data2.insert(0, b) 11 c = [data1, data2] 12 with open('data3_processed.txt', 'w', encoding='utf-8') as f2: 13 for data in zip(*c): 14 f2.write('{0}\t{1}\n'.format(*data))
4.实验任务4
1 with open('data4.txt', 'r+', encoding='utf-8') as f1: 2 inf1 = f1.readlines() 3 inf2 = [list(i.strip().split()) for i in inf1] 4 a=inf2.pop(0) 5 inf3 = sorted(inf2, key=(lambda x: [x[2], -int(x[3])])) 6 inf3.insert(0,a) 7 with open('data4_processed.txt ','w',encoding='utf-8')as f2: 8 for m in inf3: 9 f2.write('\t'.join(m)+'\n') 10 for n in m: 11 print('%-5s\t'%n,end='') 12 print()
5.实验任务5
1 with open('data5.txt','r+',encoding='utf-8')as f1: 2 inf=f1.read() 3 line = len(inf.splitlines()) 4 word = len(inf.split()) 5 string = len(inf) 6 space=0 7 for i in inf: 8 if i==' ': 9 space+=1 10 print(f'行数:{line}') 11 print(f'单词数:{word}') 12 print(f'空格数:{space}') 13 print(f'字符数:{string}') 14 with open('data5_with_line.txt', 'w', encoding='utf-8') as f2: 15 for n in range(line): 16 f2.write(str(n+1)+' '+inf.split('\n')[n]+'\n')
实验总结:
谁来救救我,呜呜呜呜呜呜呜,真的好难(准备把自己埋了),欸~