实验6

import csv
list3=[]       #shenfenzhenghao
list1=[]     #xingming
list2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'X']
with open('data3_id.txt', 'r', encoding='utf-8') as f:
        f_reader = csv.DictReader(f)
        for line in f_reader:
            list1.append(line['姓名'])
            list3.append(line['身份证号码'])

def is_valid(x):
   ggg=list(range(len(list3)))
   for id1 in ggg:
       if len(x)!=18:
           return False
       for items in set(x):
           if list2.count(items)==0:
               return False
       return True   #去除不是18位数的身份证号 并生成对应的姓名



aaa=[]
for i in range(len(list3)):
    if is_valid(list3[i])==False:aaa.append(i)
for i in aaa:list3[i],list1[i]=0,0
for i in list3:
    if i==0:list3.remove(0);list1.remove(0)

bbb=[(int(list3[i][6:10]),i) for i in range(len(list1))]
bbb.sort()
ccc=[i[1] for i in bbb]

for i in ccc:
    print(f'{list1[i]},{list3[i][6:10]}-{list3[i][10:12]}-{list3[i][12:14]}')

 

 

import random
with open('data5.txt','r+',encoding='utf-8') as f:
    a=f.readlines()
    b=input('输入你想要抽多少组数据:')
    c=random.sample(a,int(b))
    d=''.join(c)
    print(d)
with open('20220516.txt','w+',encoding='utf-8') as book:
    book.write(d)

 

 

import random
with open('data5.txt','r+',encoding='utf-8') as f:
    a=f.readlines()
while True:
    b = eval(input('输入你想要抽多少组数据:'))
    if b!=0:
       c=random.sample(a,int(b))
       for i in c:
           print(i.strip('\n'))
           a.remove(i)
       with open('20220519.txt','a+',encoding='utf-8') as book:
          for i in c:
            book.write(i)
    else:
        break

 

posted @ 2022-05-21 14:02  戎梓瑞  阅读(2)  评论(1编辑  收藏  举报