# 实验5

with open('data1_1.txt', 'r', encoding='utf-8') as f:
n=0
for line in f:  # 逐行遍历处理
if line.strip('\n') =='':
continue
n+=1
print(f'共{n}行')

with open('data2.txt','r',encoding='utf-8') as f:
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)

with open('data3.txt','r',encoding='utf-8')as f:
list=[]
a=1
for i in data:
if a==1:
a=a+1
list.append('四舍五入后数据')
continue
else:
i=eval(i)
if i-int(i)>=0.5:
i=int(i)+1
list.append(i)
else:
i=int(i)
list.append(i)
f.close()
list1=[]
for i in range(len(list)):
if i==0:
s=data[i]='\t'+str(list[i])+'\n'
else:
s=data[i]='\t'+'\t'+str(list[i])+'\n'
list1.append(s)
with open ('data3_processed.txt','w',encoding='utf-8')as n:
n.writelines(list1)
f.close()
print(data[0])
print(data[1:])
print(list[0])
print(list[1:])

with open('data4.txt','r',encoding='utf-8') as f:
data=['']*(len(yuan)-1)
res=['']*len(yuan)
for i in range(0,len(yuan)):
yuan[i]=list(yuan[i].split('\t'))
if i>0:
yuan[i][3]=int(yuan[i][3])
data[i-1]=yuan[i]
data.sort(key=lambda s:(s[2],-s[3]))
for j in range(0, len(yuan[0])):
res[0] += str(yuan[0][j]) + '\t'
res[0] += '\n'
for i in range(0,len(data)):
for j in range(0, len(data[i])):
res[i+1] += str(data[i][j]) + '\t'
res[i+1] += '\n'
with open('data4out.txt','a',encoding='utf-8') as f:
f.writelines(res)

data=[]
s1=0
s2=0
s3=0
with open('data5.txt', 'r',encoding = 'utf-8') as f:
for i in range(len(data)):
s1+=len(data[i].split(' '))
s2+=len(data[i])
s3+=data[i].count(' ')
print('行数：',len(data));print('单词数：',s1)
print('字符数:',s2);print('空格数',s3)
for i in range(len(data)):
data[i]=str(i+1)+' '+data[i]
with open('data5out.txt', 'w',encoding = 'utf-8') as f:
f.writelines(data)

posted @ 2022-05-17 15:14  思杰  阅读(6)  评论(3编辑  收藏  举报