ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

实验5 文件应用编程

2022-05-17 01:33:19  阅读:132  来源: 互联网

标签:文件 编程 len tir1 实验 print txt open append


实验任务3

with open('data3.txt','r',encoding='utf-8') as f:
    be=[]
    b=f.readlines()
    for i in b[1::]:
        i=eval(i.strip('\n'))
        be.append(i)
    print(f'原始数据:\n{be}')
a=[]
for i in be:
    if i%1>=0.5:
        i=int(i)+1
        a.append(i)
    else:
        i=int(i)
        a.append(i)
print(f'四舍五入后数据:\n{a}')

with open('data3.processeed.txt','w') as f:
    be1=['原始数据']
    be2=be1+be
    a1=['四舍五入后数据']
    a2=a1+a
    for i in range(len(be)+1):
        f.write(str(be2[i])+'\t'+str(a2[i])+'\n')
    

实验任务4

with open('data4.txt','r') as f:
    list1=f.readlines()
    b=[]
    major=[]
    score=[]
    
    name=list1[0]
    list1.remove(name)
    for i in list1:
        i=i.split('\t')
        c=i[2]
        major.append(c)
        a=i[0]+'\t'+i[1]
        d=i[3]
        score.append(d)
        b.append(a)
    list2=sorted(list(zip(major,b,score)))
    list3=[list(i) for i in list2[0:7]]
    list4=[i[::-1] for i in list3]
    list5=sorted(list4,reverse=True)
    list6=[list(i) for i in list2[8::]]
    list7=[i[::-1] for i in list6]
    list8=sorted(list7,reverse=True)
    lista=list5+list8
   
    tir1=[]
    tir2=[]
    tir3=[]
    for j in lista:
        tir1.append(j[0])
        tir2.append(j[1])
        tir3.append(j[2])
        
with open('data4_processed.txt','w') as f:
    f.writelines(name)
    for i in range(len(tir1)):
        f.write(tir2[i]+'\t'+tir3[i]+'\t'+tir1[i]+'\n')
with open('data4_processed.txt','r') as f:
    print(f.read())

实验任务5

with open('data5.txt','r') as f:
    b=f.readlines()
with open('data5.txt','r')as f:
    a=f.read()
    kong=a.count(' ')
    words=a.split()
print(f'行数:{len(b)}')
print(f'单词数:{len(words)}')
print(f'空格数:{kong}')
print(f'字符数:{len(a)}')

with open('data5_with_line.txt','w') as f:
    x=1
    for i in b:
        i=str(x)+' '+i
        f.write(i)
        x+=1

 

 

 

标签:文件,编程,len,tir1,实验,print,txt,open,append
来源: https://www.cnblogs.com/2896798343pi/p/16254540.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有