ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验五

2022-05-14 16:32:49  阅读:6  来源: 互联网

标签:utf encoding open len 实验 txt data


with open('data3.txt','r',encoding='utf-8') as f:
    data=f.readlines()
    f.close()
with open('data3_proxessed.txt','w',encoding='utf-8') as b:
    a=['四舍五入后数据']
    for i in range(len(data)):
        data[i]=data[i].strip('\n')
    for i in data[1:]:
        a.append(str(round(eval(i))))
    for i in range(len(data)):
        b.write(data[i]+'\t'+a[i]+'\n')
    b.close()
    ys=[]
    zz=[]
    for i in data[1:]:
        i=eval(i.strip('\n'))
        ys.append(i)
    print(f'原始数据:\n',ys)
    for i in data[1:]:
        i=round(eval(i))
        zz.append(i)
    print('四舍五入后数据:\n',zz)

 

 

with open('data4.txt','r',encoding='utf-8') as f:
    data=f.read().strip('\n')
    a=data.split('\n')
    m=a.pop(0)
    x=[]
    for i in a:
        a=i.split('\t')
        x.append(a)
    y=sorted(x,key=lambda x:(x[2],x[3]),reverse=True)
    with open('data4_processed.txt','w',encoding='utf-8') as g:
        g.write(m+'\n')
        print(m)
        for n in y:
            g.write('\t'.join(n))
            g.write('\n')
            print('\t'.join(n))

 

 

 

with open('data5.txt','r',encoding='utf-8') as f:
    data=f.read().strip('\n')
    a=data.split('\n')
    b='\n'.join(a)
    blank_counts=0
    with open('data5_with_line.txt','w',encoding='utf-8') as g:
        for i in range(len(a)):
            for x in a[i]:
                if x==' ':
                    blank_counts+=1
                else:
                    blank_counts=blank_counts
            g.write(f'{i+1} {a[i]}\n')
print(f'''行数:{len(a)}
单词数:{len(b)}
空格数:{blank_counts}
字符数:{len(data)}''')

 

 

标签:utf,encoding,open,len,实验,txt,data
来源: https://www.cnblogs.com/zyfff/p/16259089.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

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

ICode9版权所有