实验四
task6
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()


task7.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)


task7.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)



浙公网安备 33010602011771号