实验五

task3

with open('C:\\Users\\Administrator\\Desktop\\实验5\\实验5数据文件\\data3.txt','r',encoding='utf-8') as f:
    data=f.read().split('\n')
    list1=[]
    list2=[]
    for i in data:
        list1.append(i)
        if i-int(i)<0.5:
            x=int(i)
            list2.append(x)
        else:
            y=int(i)+1
            list2.append(y)

print('原始数据为',list1)
print('四舍五入后数据为',list2)

 

 

task4

with open('C:\\Users\\Administrator\\Desktop\\实验5\\实验5数据文件\\data4.txt','r') as f:
    data=f.readlines()
    list=[]
    list.append(data)
    for i in list:
        if i[2]='Acting':
            list1=[]
            list1.append(i)
        else:
            list2=[]
            list2.append(i)

 

with open('data4.txt', 'r',encoding ='utf-8') as f:
    data = f.read().split('\n')
    for i in range(1,len(data)):
     data[i]=data[i].split('\t')
     data1=data[1:].copy()
     data1.sort(key=lambda x:(x[2],-int(x[3])),reverse = False)
     data[0]+='\n'
     for i in range(len(data1)):
         for j in range(len(data1[i])):
             if j==3:
                 data1[i][j]+=str(data1[i][j])+'\n'
             else:
                 data1[i+1]+=str(data1[i][j])+'\n'
with open("data4_processed.txt", 'w', encoding ='utf-8') as f:
     f.writelines(data)
with open('data4_processed.txt', 'r',encoding = 'utf-8') as f:
     data = f.read()
     print(data)

 

task5

data=[]
s1=0
s2=0
s3=0
with open('data5.txt', 'r',encoding = 'utf-8') as f:
    data=f.readlines()
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('data5_with_line.txt', 'w',encoding = 'utf-8') as f:
     f.writelines(data)
with open('data5_with_line.txt', 'r',encoding = 'utf-8') as f:
    data1=f.readlines()
    print(data1)

 

posted @ 2022-05-17 12:28  杪秋有四0904  阅读(9)  评论(1编辑  收藏  举报