Python类
说到分享自然不能少了这个
但其实也没什么好说的我就分享一个我做的小项目给大家看一下吧,就是上一节说的那个造假姓名数据,自己想一次性 生成多少就是多少
import random
fN=[] #姓
lN1=[] #一个字的名
lN2=[] #两个字的名
with open('C:/Users/zhaopf.DIGIWIN/Desktop/firstName.txt') as fFile:#姓
fN=fFile.read().split('
')
# print(fN)
# for line in fFile.readline():
# for l in line:
# if len(l)==1:
# lN1.append(l)
with open('C:/Users/zhaopf.DIGIWIN/Desktop/lastName.txt') as lFile:#名
lN=lFile.read().split('
')
for ln in lN:
if len(ln)==1:
lN1.append(ln)
else:
lN2.append(ln)
# print(lN1)
# print(lN2)
class FakeUser:
fullName=''
sex=''
def fakeName(self,num,isOne=False,isTwo=True):
while num>=0:
if isOne:
FakeUser.fullName=random.choice(fN)+random.choice(lN1)
yield FakeUser.fullName
num-=1
# 这个要注意一下就是说这个一定要加个类或者是self,不然就不确定是哪一个的变量,
# 如果上面没有定义并且下面也不写self就会报错
elif isTwo:
FakeUser.fullName=random.choice(fN)+random.choice(lN2)
yield FakeUser.fullName
num -= 1
else:
FakeUser.fullName=random.choice(fN)+random.choice(lN1)+random.choice(lN2)
yield FakeUser.fullName
num -= 1
def fakeSex(self,num):
while num >= 0:
sexs=['男','女','未知']
FakeUser.sex=random.choice(sexs)
yield FakeUser.sex
num -= 1
class SonsUser(FakeUser):
number=''
def numberFollow(self,num,few=True,aLot=False):
while num >= 0:
if few:
SonsUser.number=random.randrange(1,50)
yield SonsUser.number
num -= 1
elif aLot:
SonsUser.number=random.randrange(200,1000)
yield SonsUser.number
num -= 1
su1=SonsUser()
num=66
listName=su1.fakeName(num,False,True)
listSex=su1.fakeSex(num)
listFollow=su1.numberFollow(num,few=False,aLot=True)
while num>0:
print("the name of this user is {} and age is {}".format(listName.__next__(),listSex.__next__()))
print("the follow number of this user is {}".format(listFollow.__next__()))
num -= 1