SAKURA-QiQi

导航

实验五

task3

# Created on 刘杰的iPad.
with open('data3.txt', 'r+', encoding = 'utf-8')as f:
    data = f.readlines()
    f.seek(0)
    f.truncate()        

    data1 = []
    for i in range(len(data)):
        data[i] = data[i].rstrip('\n')
        if i ==0:
            data1.append('四舍五入后的数据')
        else:
            if (float(data[i]) * 10) % 10 >= 5:
                x = int(float(data[i])) + 1
            else:
                x = int(float(data[i]))
            data1.append(x)
        f.writelines(f'{data[i]: <10}\t{data1[i]: <10}\n')

data.pop(0)
data1.pop(0)
print(f'原始数据:\n{data}')
print(f'四舍五入后的数据:\n{data1}'

 

 

 

 

 task4

# Created on 刘杰的iPad.
with open('data4.txt', 'r', encoding='utf-8')as f:
    data = f.readlines()
for i in range(len(data)):
    data[i] = data[i].split('\t')
x = data.pop(0)
data.sort(key=lambda ele:ele[3],reverse=True)
data.sort(key=lambda ele:ele[2])
data = [x] + data       

with open('实验5/data4_processed.txt', 'w+', encoding='utf-8')as f:
    for i in range(len(data)):
        f.writelines('\t'.join(data[i]) + '')
    f.seek(0,0)
    data1 = f.readlines()
    print(''.join(data1))

 

 

 

 task5

# Created on 刘杰的iPad.
with open('data5.txt', 'r', encoding='utf-8')as f:
    data = f.readlines()
data_str = ''.join(data)
line_number = len(data)
word_number = len(data_str.split())
space_number = data_str.count(' ')
chars_number = len(data_str)
print(f'行数:{line_number}')
print(f'单词数:{word_number}')
print(f'空格数:{space_number}')
print(f'字符数:{chars_number}')
with open('实验5/data5_with_line.txt', 'w', encoding='utf-8')as f:
    for i in range(len(data)):
        data[i] = str(i+1)+'\t'+data[i]
    f.writelines(''.join(data))

 

 

 

posted on 2022-05-16 19:25  QiQi-sakura  阅读(6)  评论(1编辑  收藏  举报