task3.py
1 with open('data3.txt', 'r', encoding='utf-8') as f: 2 data = f.read().split('\n') 3 yuanshishuju = data.pop(0) 4 data1 = [eval(i) for i in data] 5 data2 = [round(i) for i in data1] 6 print(f'{yuanshishuju}:\n{data1}') 7 print(f'四舍五入后数据:\n{data2}') 8 with open('jishiben.txt','w',encoding='utf-8') as f: 9 list=['原始数据']+[str(i) for i in data1] 10 list1=['四舍五入后数据']+[str(i) for i in data2] 11 for i in range(len(data)): 12 f.write(f'{list[i]}\t{list1[i]}\n')
task4.py
1 with open('data4.txt', 'r+', encoding='utf-8') as f: 2 str = [i.strip('\n').split('\t') for i in f] 3 a = str.pop(0) 4 str.sort(key=lambda x: (x[2], -int(x[-1]))) 5 print('\t'.join(a)) 6 for i in str: 7 print('\t'.join(i)) 8 with open('data4_progressed.txt', 'w') as f: 9 f.write('\t'.join(a)) 10 f.write('\n') 11 for i in str: 12 f.write('\t'.join(i)) 13 f.write('\n')
task5.py
1 with open('data5.txt','r',encoding='utf-8')as f: 2 data=''.join([i for i in f]) 3 4 print(f'行数:{len(data.splitlines())}') 5 print(f'单词数:{len(data.split())}') 6 print(f'空格数:{data.count(" ")}') 7 print(f'字符数:{len(data)}')