美文网首页
python 数据生成器 Faker

python 数据生成器 Faker

作者: alue | 来源:发表于2022-08-15 17:02 被阅读0次

开发过程中,经常遇到需要mock数据的场景。以前我都是自己用随机数,或者用网络上的API来生成数据。今天无意中发现了一个非常好用的库 ——Faker, 完美解决了数据生成的问题。

Faker用法简单,能够生成常见的字段,例如姓名、地址、日期、电话、邮箱、颜色等格式的字段,极大提升了开发效率。

pip install Faker
fake = Faker('zh-CN')
fake.name() # '杨金凤'
fake.address() # '山东省丽娟市闵行南京路H座 799357'
fake.text() # 长段文字
fake.date_this_year() # datetime.date(2022, 7, 13)
fake.date_between(date_start,date_end) # datetime.date(2011, 8, 12)

fake.company() # '襄樊地球村传媒有限公司'
fake.color() # '#167f0c'
fake.job() # 'Flash设计/开发'
fake.phone_number() # '18765454823'
fake.ascii_company_email() # lhan@mofan.org
fake.ascii_free_email() # 'achang@gmail.com'

fake.profile() 
# {'job': '娱乐服务员', 'company': '鑫博腾飞网络有限公司', 'ssn': '420703199303196044', 'residence': '甘肃省哈尔滨市静安石家庄街Y座 966165', 'current_location': (Decimal('49.9340065'), Decimal('84.426417')), 'blood_group': 'A-', 'website': ['http://zhouhou.cn/', 'https://sun.cn/', 'http://ming.cn/', 'http://chentian.cn/'], 'username': 'qiangyan', 'name': '郝建国', 'sex': 'M', 'address': '福建省宜都市和平张街j座 136877', 'mail': 'vzeng@gmail.com', 'birthdate': datetime.date(1971, 3, 29)}



相关文章

网友评论

      本文标题:python 数据生成器 Faker

      本文链接:https://www.haomeiwen.com/subject/dillgrtx.html