实验五

3
with open('data3.txt','r') as f:
        yuanshi=f.readlines()
yuanlist=[]
pro=[]
for i in yuanshi:
    j=i.strip('\n')
    if j=='原始数据':
        pass
    else:
        yuanlist.append(eval(j))
        if eval(j)-int(eval(j))>=0.5:
            pro.append(int(eval(j)+1))
        else:
            pro.append(int(eval(j)))
print('原始数据:\n',yuanlist)
print('四舍五入后数据:\n',pro)
l=['原始数据        四舍五入后数据']
for i in range(len(pro)):
    l.append('%-4s'%(str(yuanlist[i]))+'             '+'%-4s'%(str(pro[i])))
with open('data3_processed.txt','w') as f:
    f.writelines('\n'.join(l))

 

4

with open('data4.txt','r') as f:
    list=f.readlines()
    del list[0]
l=[]
for i in list:
    j=i.strip('\n').split('\t')
    l.append(j)
li=sorted(l,key=lambda x:(x[2],-int(x[3])))
with open('data4_processed.txt','w') as f:
    f.writelines('学号      姓名       专业        分数\n')
    print('学号      姓名       专业        分数')
    for i in li:
        for j in i:
            print('%-s'%(j),end='    ')
            f.writelines(f'{j:3s}')
            f.writelines('    ')
        f.writelines('\n')
        print('')

5

with open('data5.txt','r') as f:
    txt=f.read()
    hang=len(txt.splitlines())
print(f'行数:{hang}')
cishu=len(txt.splitlines())-1+txt.count(' ')
print(f'单词数:{cishu}')
kong=txt.count(' ')
zifu=len(txt)
print(f'空格数:{kong}')
print(f'字符数:{zifu}')
l=txt.splitlines()
with open('data5_with_line.txt','w') as f:
    for i in range(len(l)):
        f.write(str(i+1)+' '+l[i]+'\n')

 

posted @ 2022-05-12 22:34  沉迷学习ing  阅读(11)  评论(1编辑  收藏  举报