实验4 文件应用编程

6.实验任务6

with open('data6_1.txt','r',encoding = 'utf-8') as f:
    c = f.read().split('\n')

cc = [str(n).split('\t') for n in c]
cc.sort(key = lambda x: x[2],reverse = True)
with open('data6_2.txt','w',encoding = 'utf-8') as f:
    for line in cc:
        for n in line:
            f.write(n+'\t')
            print(n+'\t',end = '')
        f.write('\n')
        print()

 

7.实验任务7

7-1

import random
with open('data7.txt','r',encoding = 'gbk') as f:
    data = f.read().split('\n')
n = eval(input('please enter number:'))
m = set()
while len(m)<n:
    c = random.randint(0,len(data)-1)
    m.add(str(data[c]))
with open('lucky.txt','w+',encoding = 'utf-8') as f:
    for line in m:
        f.write(line + '\n')
        print(line)

 

7-2

import datetime
time = datetime.datetime.now()
time = time.strftime('%Y%m%d')
import random
with open('data7.txt','r',encoding = 'gbk') as f:
    data = f.read().split('\n')
n = eval(input('please enter number'))
m = set()
while len(m)<n:
    cc = random.randint(0,len(data)-1)
    m.add(str(data[cc]))
with open(f'{time}.txt','w+',encoding = 'utf-8') as f:
    for line in m:
        f.write(line + '\n')
        print(line)

 

posted @ 2021-05-16 21:51  AOIL  阅读(61)  评论(0编辑  收藏  举报