生成随机字母或数字

import random
import string
'''
ascii_letters是生成所有字母,从a-z和A-Z,digits是生成所有数字0-9.s
'''
def rand_str ( num , length = 7 ) :
     f = open ( 'Activation_code.txt' , 'w' )
     for i in range ( num ) :
         chars = string . ascii_letters + string . digits # 生成的随机字母加数字
         s = [ random . choice ( chars ) for i in range ( length ) ]
         f . write ( '{0}\n' . format ( '' . join ( s ) ) ) # 这代码看不懂
     f . close ( )
 
 
if __name__ == '__main__' :
     rand_str ( 200 )

posted @ 2021-07-03 15:04  索匣  阅读(280)  评论(0编辑  收藏  举报