在工作或学习中如果你有经常要使用一些生成器方法来生成随机数、手机号、名称以及连续数字等需求,那么Python的Faker随机库将是你的不二之选,它将极大提高你的工作或学习效率
import random
from faker import Faker
fake = Faker('zh_cn')
def random_name():
"""随机姓名"""
return fake.name()
def random_name_male():
'''男性姓名'''
return fake.name_male()
def random_name_female():
'''女性姓名'''
return fake.name_female()
def random_email():
"""随机email"""
return fake.email()
def random_password(length=10, special_chars=True, digits=True, upper_case=True, lower_case=True):
'''
随机密码(可指定密码策略)
@param length:密码长度;
@param special_chars:是否能使用特殊字符;
@param digits:是否包含数字;
@param upper_case:是否包含大写字母;
@param lower_case:是否包含小写字母
'''
return fake.password(length=length, special_chars=special_chars, digits=digits, upper_case=upper_case, lower_case=lower_case)
def random_phone_number():
"""随机手机号"""
return fake.phone_number()
def random_ean8():
'''8位条码'''
return fake.ean8()
def random_ean13():
'''13位条码'''
return fake.ean13()
def random_company():
'''公司名'''
return fake.company()
def random_currency_code():
'''货币代码'''
return fake.currency_code()
def random_date_time():
'''随机日期时间'''
return fake.date_time()
def random_date_time_this_month(before_now=True, after_now=False, tzinfo=None):
'''
本月的某个日期
@param before_now 是否在今天之前的日期
@param after_now 是否在今天之后的日期
@param tzinfo
'''
return fake.date_time_this_month(before_now=before_now, after_now=after_now, tzinfo=tzinfo)
def random_date_time_this_year(before_now=True, after_now=False, tzinfo=None):
'''
本年的某个日期
@param before_now 是否在今天之前的日期
@param after_now 是否在今天之后的日期
@param tzinfo
'''
return fake.date_time_this_year(before_now=before_now, after_now=after_now, tzinfo=tzinfo)
def random_date_time_this_decade(before_now=True, after_now=False, tzinfo=None):
'''
本年代内的一个日期
@param before_now 是否在今天之前的日期
@param after_now 是否在今天之后的日期
@param tzinfo
'''
return fake.date_time_this_decade(before_now=before_now, after_now=after_now, tzinfo=tzinfo)
def random_date_time_this_century(before_now=True, after_now=False, tzinfo=None):
'''
本世纪一个日期
@param before_now 是否在今天之前的日期
@param after_now 是否在今天之后的日期
@param tzinfo
'''
return fake.date_time_this_century(before_now=before_now, after_now=after_now, tzinfo=tzinfo)
def random_date_time_between(start_date="-30y", end_date="now", tzinfo=None):
'''
两个时间间的一个随机时间
@param start_date 开始时间 -30y表示三十年内
@param end_date 结束日期 now表示现在的时间
@param tzinfo
'''
return fake.date_time_between(start_date=start_date, end_date=end_date, tzinfo=tzinfo)
def random_timezone():
'''时区'''
return fake.timezone()
def random_time(pattern="HH:MM:SS"):
'''随机时间,格式:HH:MM:SS'''
return fake.time(pattern=pattern)
def random_am_pm():
'''随机上午下午'''
return fake.am_pm()
def random_month():
'''随机月份'''
return fake.month()
def random_year():
'''随机年'''
return fake.year()
def random_day_of_week():
'''随机星期几'''
return fake.day_of_week()
def random_day_of_month():
'''随机月中某一天'''
return fake.day_of_month()
def random_time_delta():
'''随机时间延迟'''
return fake.time_delta()
def random_date_object():
'''随机日期对象'''
return fake.date_object()
def random_time_object():
'''随机时间对象'''
return fake.time_object()
def random_unix_time():
'''随机unix时间(时间戳)'''
return fake.unix_time()
def random_date(pattern="YY-mm-dd"):
'''随机日期,格式:YY-mm-dd'''
return fake.date(pattern=pattern)
def random_date_time_ad():
'''公元后随机日期'''
return fake.date_time_ad(tzinfo=None)
def random_uuid4():
'''随机uuid'''
return fake.uuid4()
def random_uuid():
'''随机uuid'''
return fake.uuid4().replace("-", "")
def random_locale():
'''随机本地代码'''
return fake.locale()
def random_phonenumber_prefix():
'''运营商号段,手机号码前三位'''
return fake.phonenumber_prefix()
def random_user_agent():
'''伪造UA'''
return fake.user_agent()
def random_ipv4():
"""随机IPV4地址"""
return fake.ipv4()
def random_str(min_chars=0, max_chars=8):
"""长度在最大值与最小值之间的随机字符串"""
return fake.pystr(min_chars=min_chars, max_chars=max_chars)
def random_num(length):
"""随机数字"""
return fake.random_number(length)
if __name__ == '__main__':
print(random_str(min_chars=5, max_chars=5))
print(random_num(length=5))
print(random_date_time())
print(random_unix_time())
print(random_date_time_between(start_date="-5h", end_date="now"))
print(random_password(length=6, special_chars=False))
print(random_company())
print(random_name())
Python实现代码中对于每个方法都有详细的中文说明,可以直接copy代码到本地运行起来,这个随机库的运用比较简单,但很好用~
如果今天的分享对你有帮助的话,请毫不犹豫:关注、分享、点赞、在看、收藏呀~ 你的鼓励将会是我创作的最大动力。