实验五

task3
def main():
    pass

if __name__ == '__main__':
    main()
with open('data3.txt','r')as f:
    data=f.read().split('\n')
del data[0]
a=list(data)
b,m=[],[]
for i in data:
    i=float(i)
    m.append(i)
    b.append(round(i))
c=['原始数据\t四舍五入后数据\t\n']
for i,j in zip(a,b):
    c.append(str(i)+'\t'+str(j)+'\n')
with open('data3_1.txt','w')as f:
    f.write(''.join(c))
print('原始数据')
print(m)
print('四舍五入后数据')
print(b)

 

 task.4

 1 with open('data4.txt','r',encoding='utf-8') as f:
 2     a=f.readlines()
 3 b=[]
 4 for i in a:
 5     x=i.strip('\n').split('\t')
 6     b.append(x)
 7 c=b.pop(0)
 8 for i in b:
 9     b.sort(key=lambda i:(i[2],-int(i[-1])))
10 print('\t'.join(c))
11 for i in b:
12     print('\t'.join(i))
13 with open('data4_processed.txt','w') as f:
14     f.write('\t'.join(c))
15     for i in b:
16         f.write('\n')
17         f.write('\t'.join(i))
复制代码

运行结果:

 

 

 task.5

def main():
    pass

if __name__ == '__main__':
    main()
with open('data5.txt','r',encoding='UTF-8') as f:
    a=f.read()
x=len(a.splitlines())
y=len(a.split())
z=a.count(' ')
b=len(a)
print('行数:',x)
print('单词数:',y)
print('空格数:',z)
print('字符数:',b)
m=1
with open('data5_with_line.txt','w') as f:
    for i in a.splitlines():
        f.write(str(m))
        f.write(' ')
        f.write(i)
        f.write('\n')
        m=m+1

 

 

posted @ 2022-05-16 22:53  黄海祥  阅读(8)  评论(3编辑  收藏  举报