其他分享
首页 > 其他分享> > 实验六-数据文件

实验六-数据文件

作者:互联网

def is_valid(x):
    if len(x)!=18:
        return False
    for i in x:
        if not (i.isdigit() or i=='X'):
            return False
    else:
        return True
with open('data3_id.txt','r',encoding='utf-8') as f:
    data=f.readlines()
    del data[0]
    line=[i.rstrip('\n').split(',') for i in data]
    valid=[]
    for i in line:
        if is_valid(i[1])==True:
            valid.append(i)
name=[]
number=[]
l2=[]
for i in valid:
    name.append(i[0])
    number.append(i[1])
n1=[i[6:14] for i in number]
l1=list(zip(name,n1))
for i in l1:
    a=list(i)
    l2.append(a)
l3=sorted(l2,key=(lambda x:x[1]),reverse=False)
for i in l3:
    b=''.join(i)
    print(f'{b[0:2]},{b[2:6]}-{b[6:8]}-{b[8:10]}')

import random
x=list(range(1,81))
n=int(input('请输入随机抽点人数:'))
with open('data5.txt','r',encoding='utf-8') as f:
    data=f.readlines()
    d1=[]
    for i in data:
        d1.append(i.strip('\n'))
    d2=random.sample(d1,n)

for i in d2:
    print(i)
import datetime
t=datetime.datetime.now()
f1=t.strftime('%Y%m%d')+'.txt'
with open(f1,'w',encoding='utf-8') as f:
    for i in d2:
        f.write(i+'\n')

import random
x=list(range(1,81))
with open('data5.txt','r',encoding='utf-8') as f:
    data=f.readlines()
    d1=[]
    for i in data:
        d1.append(i.rstrip('\n'))
import datetime
t=datetime.datetime.now()
f1=t.strftime('%Y%m%d')+'.txt'
print('{:=^50}'.format('抽点开始'))

while True:
    n=int(input('请输入随机抽点人数:'))
    if n==0:
        print('{:=^50}'.format('抽点结束'))
        break
    d2=(random.sample(d1,n))

    for i in d2:
        print(i)
    with open(f1,'a',encoding='utf-8') as f:
        for i in d2:
            f.write(i+'\n')

 

标签:数据文件,encoding,d2,datetime,实验,data,append,d1
来源: https://www.cnblogs.com/hhkki/p/16308094.html