#案例:造大量测试数据!*.txt,*.csv
import random
#函数原型:String createPhone(String starts);
#函数功能:创建一个随机手机号!
#形参starts:你想要创建的手机号的开头数字的列表。"136,137,139"
def createPhone(starts):
a=starts.split(",")
bufen1=random.choice(a)
bufen2=random.sample("0123456789",8)
random.shuffle(bufen2)
bufen2="".join(bufen2)
return bufen1+bufen2
#调用函数
# phone=createPhone("136,137,139,150")
# print(phone)
#函数原型:String createPwd(int n)
#函数功能:生成一个n位的随机密码。要求密码:大写、小写、数字都要有!
def createPwd(n):
daxie="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
xiaoxie="abcdefghijklmnopqrstuvwxyz"
suzi="0123456789"
num_daxie=random.randint(1,n-2)
num_xiaoxie=random.randint(1,n-num_daxie-1)
num_suzi=n-num_daxie-num_xiaoxie
bufen_daxie=random.sample(daxie,num_daxie)
bufen_xiaoxie=random.sample(xiaoxie,num_xiaoxie)
bufen_suzi=random.sample(suzi,num_suzi)
a=bufen_daxie+bufen_xiaoxie+bufen_suzi
random.shuffle(a)
return "".join(a)
# pwd=createPwd(10)
# print(pwd)
fp=open("C:/A/1.txt","w")//文件夹自己创建
n=50
i=1
while i<=n:
phone=createPhone("136,137,139,150")
pwd=createPwd(10)
t=phone+","+pwd
fp.write(t)
fp.write("\n")
i=i+1
fp.close()
=========================================================================
函数使用版
#案例:造大量的测试数据
import random
import string
#函数功能:生成一个手机号,手机号是以"135" "136" "138"," 134","139"开头!
def createPhone():
str_start = random.choice(['135', '136', '138','134','139']) #"135"
str_end = ''.join(random.sample('0123456789', 8)) #"45120123"
return str_start + str_end
#函数功能:生成一个n位随机密码(要求:大写字母、小写字母、数字都要有。
#函数原型:String createPassword(int n)
def createPassword(n):
src_digits = string.digits #获取所有的数字字符。"0123456789"
src_uppercase = string.ascii_uppercase #获取所有的大写字母:"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
src_lowercase = string.ascii_lowercase #获取所有的小心字母:"abcdefghijklmnopqrstuvwxyz"
digits_num = random.randint(1,n-2) #因为密码至少需要一个大写字母、一个小写字母!
uppercase_num = random.randint(1,n-digits_num-1)
lowercase_num = n - (digits_num + uppercase_num)
password = random.sample(src_digits,digits_num) + random.sample(src_uppercase,uppercase_num) + random.sample(src_lowercase,lowercase_num)
random.shuffle(password) #打乱顺序
new_password = ''.join(password)
return new_password
#函数功能:在文件filePath中生成num个"手机号,密码,确认密码"的数据!密码长度是n!
def createData(filePath,num,n):
f = open(filePath,"a",encoding="utf-8")
for i in range(num):
pwd=createPassword(n)
line=createPhone()+","+pwd+","+pwd+"\n"
f.writelines(line)
f.close()
createData("C:/A/1.txt",50,10)//文件夹自己创建
版权声明:本文为qq_40531965原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。