实验五
作者:互联网
1 with open('D://aa//data3.txt','r',encoding='utf-8')as f: 2 data=f.read().split('\n') 3 list1=data[1:11] 4 print("原始数据:") 5 print(list1) 6 list2=[] 7 for i in list1: 8 num=float(i) 9 y=int(num) 10 x=num-y 11 if x>=0.6: 12 y+=1 13 list2.append(y) 14 else: 15 y=y 16 list2.append(y) 17 print("四舍五入后数据:") 18 print(list2) 19 with open('D://aa//data3_processed.txt','w',encoding='utf-8') as f: 20 list1.insert(0,'原始数据') 21 list2.insert(0,'四舍五入后数据') 22 for i in range(len(list1)): 23 f.write(f"{list1[i]}\t{list2[i]}\n")
1 with open('D://aa//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('D://aa//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')
1 with open('D://aa//data5.txt', 'r', encoding='utf-8') as f: 2 list=''.join([i for i in f]) 3 hang=len(list.splitlines()) 4 words=len(list.split()) 5 kong=list.count(' ') 6 zifu=len(list) 7 print('行数:',hang) 8 print('单词数:',words) 9 print('空格数:',kong) 10 print('字符数:',zifu) 11 with open('D://aa//data5_with_line.txt','w',encoding='utf-8') as f: 12 for i in range(hang): 13 f.write('{} {}'.format(i+1,list.split('\n')[i])) 14 f.write('\n')
标签:aa,list1,list2,write,实验,print,txt 来源: https://www.cnblogs.com/7762qimei/p/16272002.html