十位随机验证码

练习:

创建一个文件,在里面存放10000个十位随机验证码

import random

f = open('code2.txt','w',encoding='utf-8')

for x in range(100000):
    
    str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789'

    list = ''
    
    for y in range(10):
       
        a = random.choice(str)
        
        list += a
    
    f.write(list+'\n')

f.close()

获取上述中的文件,然后判断每个验证码中数字出现的次数。

f = open('code2.txt','r',encoding='utf-8')

a=f.readlines()

for x in a:
    
    i = 0
    
    for y in x.strip():
      
        if y.isdigit():
         
            i+=1
        
        else:
           
            pass
   
    f1 = open('code3.txt', 'a', encoding='utf-8')
    
    f1.writelines(str(i)+'\n')
    
    f1.close()

f.close()

统计所有的数字出的次数:

f = open('code2.txt','r',encoding='utf-8')

num_count = {}

a = f.read()

num_count['0'] = a.count('0')

num_count['1'] = a.count('1')

num_count['2'] = a.count('2')

num_count['3'] = a.count('3')

num_count['4'] = a.count('4')

num_count['5'] = a.count('5')

num_count['6'] = a.count('6')

num_count['7'] = a.count('7')

num_count['8'] = a.count('8')

num_count['9'] = a.count('9')

print(num_count)

运行结果如下:

{'0': 15978, '1': 16399, '2': 16539, '3': 16163, '4': 16269, '5': 16102, '6': 16275, '7': 15937, '8': 15934, '9': 16001}

猜你喜欢

转载自blog.csdn.net/qq_39138295/article/details/80905068