实验5
测试1
with open('data1_1.txt', 'r', encoding = 'utf-8') as f: data = f.readlines() n = 0 for line in data: if line.strip('\n') == '': continue n += 1 print(f'共{n}行')
测试2
with open('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)
测试3
with open('data3.txt', 'r', encoding = 'utf-8') as f:
data = f.read().split('\n')
data0=data.copy()
for s in range(len(data0)):
data0[s]=data0[s]+'\t'
print(data0)
for i in range(1,len(data)):
data[i]=eval(data[i])
if data[i]-int(data[i])>=0.5:
data[i]=int(data[i])+1
else:
data[i]=int(data[i])
data[0]='四舍五入后数据'
for j in range(len(data)):
data[j]=str(data[j])+'\n'
print(data)
data2=[]
for x,y in zip(data0,data):
z=x+y
data2.append(z)
print(data2)
with open('data3.txt', 'w', encoding = 'utf-8') as f:
f.write(''.join(data2))
测试4
with open('data4.txt', 'r', encoding = 'utf-8') as f: data = f.read().split('\n') for line in range(len(data)): data[line]=data[line].split('\t') data1=data[1:] for i in data1: i[0],i[2]=i[2],i[0] data1=sorted(data1) for i in data1: i[0],i[2]=i[2],i[0] i[0],i[3]=i[3],i[0] data2=data1[:-2] data2.sort(reverse=True) for i in data1: i[0],i[3]=i[3],i[0] data2.append(data1[-1]) data2.append(data1[-2]) data3=[] for j in data2: data3.append('\t'.join(j)) for s in range(len(data3)): data3[s]=data3[s]+'\n' with open('data4.txt', 'w', encoding = 'utf-8') as f: f.write(''.join(data3))
测试5
with open('data5.txt', 'r', encoding = 'utf-8') as f: data = f.read() n1=0 n2=0 n3=0 n4=0 for i in data: n4=n4+1 if i==' ': n3=n3+1 data=data.split('\n') n1=len(data) for line in range(len(data)): data[line]=data[line].split(' ') n2=n2+len(data[line]) print('行数:'+str(n1)) print('单词数:'+str(n2-1)) print('空格数:'+str(n3)) print('字符数:'+str(n4)) with open('data5.txt', 'r', encoding = 'utf-8') as f: data = f.read().split('\n') for j in range(len(data)): data[j]=str(j+1)+':'+data[j] with open('data5.txt', 'w', encoding = 'utf-8') as f: f.write('\n'.join(data))