实验五
作者:互联网
task2
with open('C:\\Users\\86131\\Desktop\\实验5数据文件\\实验5数据文件\\data2.txt','r',encoding='utf-8') as f: data=f.read().split('\n') unique_line=[] for line in data: if data.count(line)==1: unique_line.append(line) print(f'共{len(unique_line)}独特行') for i in unique_line: print(i)
task3
with open('C:\\Users\\86131\\Desktop\\实验5数据文件\\实验5数据文件\\data3.txt','r',encoding='utf-8') as f: data=f.readlines() a=[] for i in data[1:]: a.append(i.strip('\n')) b=[] for i in a: if '.' not in i: b.append(i) else: if int(i[i.index('.')+1])<5 : b.append(i[:i.index('.')]) else: b.append(str(int(i[:i.index('.')])+1)) print(a) print(b) with open('data3_processed.txt','w',encoding='utf-8') as f: for i in range(0,len(a)): f.write(f'{a[i]:<5}') f.write(' ') f.write(b[i]) f.write('\n')
task4
with open('C:\\Users\\86131\\Desktop\\实验5数据文件\\实验5数据文件\\data4.txt','r',encoding='utf-8') as f: a=f.readlines() b=[] for i in a: b.append(i.strip('\n').split('\t')) print(a) t=b[1:] t.sort(key=lambda x:(x[2],-int(x[3]))) with open('data4_processed.txt','w',encoding='utf-8') as f: for i in b[0]: f.write(f'{i:<10}') f.write('\n') for i in t: for x in i: f.write(f'{x:<10}') f.write('\n')
task5
with open('C:\\Users\\86131\\Desktop\\实验5数据文件\\实验5数据文件\\data5.txt','r',encoding='utf-8') as f: a=f.readlines() x=len(a) t=''.join(a) kongge=t.count(' ') danci=len(t.split()) zifu=len(t) print(x) print(danci) print(kongge) print(zifu) with open('data5_with_line_number.txt','w',encoding='utf-8') as f: for i in range(len(a)): f.write(f'{i+1:<5}') f.write(a[i])
标签:数据文件,encoding,实验,print,line,txt,open 来源: https://www.cnblogs.com/ljb202183360049/p/16278606.html