Requirement: Generate random non-duplicate verification code.
Code:
#!/usr/bin/env python#Encoding:utf-8"""@author: The great man KAMIL@FILE:200NUMBER.PY@TIME:2016/4/13 23:33"""Importrandom,stringdefRand_str (num,length = 7): F= Open ("Activation_code2.txt","WB") forIinchrange (num): Chars= String.ascii_letters +string.digits S= [Random.choice (chars) forIinchrange (length)] F.write (bytes ("'. Join (s) +'\ n'),'Utf-8'))#f.write ('. Join (s) + ' \ n ') py2f.close ()if __name__=="__main__": Rand_str (200)
will be written in the file line by row, involving knowledge points: F.open F.writer random
#' str ' does not support the buffer interface in Python3 errorWith open ("Test.txt") as Fp:line=Fp.readline () with open ("Test.out",'WB') as Fp:fp.write (line)#solution 1 needs to encode str in python3x,With open ("Test.txt") as Fp:line=Fp.readline () with open ("Test.out",'WB') as Fp:fp.write (bytes (line,'Utf-8'))#Solution 2 does not want to write in B (binary) mode, so this error can be avoided by using the T (text, which is the default mode for write) mode write.With open ("Test.txt") as Fp:line=Fp.readline () with open ("Test.out",'WT') as FP:#with open ("Test.out", ' W ') as FP:Fp.write (line)
Random Verification Code generation (Python implementation)