大家好,我是欧K~
今天逛GitHub的时候发现一个很牛X的库 -- Faker,这个库能给生成一些虚假的信息,包括个人信息或者一些测试数据,如果我们在开发中需要利用一些假数据来做测试,那么这个库就派上用场啦~
1. 安装
直接pip安装即可:
pip install Faker
2. 使用
2.1 简单使用
创建虚拟姓名、电话号码、地址等信息:
from faker import Faker fake = Faker() fake.name() # 'Jonathon Dixon' fake.phone_number() # '262-035-1927' fake.address() # '590 Hart Motorway\nSergioshire, NM 76453'
2.2 国内信息
这个库默认显示是国外的信息,接下来我们设置一下国内信息:
from faker import Faker fake = Faker('zh_CN') fake.name() # '汪雪梅' fake.phone_number() # '18535612607' fake.address() # '重庆市秀云县静安潜江街X座 690499'
是不是可以假乱真~
当然,如果要生成其他语种或地区的数据,也是可以的,直接替换Faker参数即可,以下是这个库所支持的语种:
ar_EG - Arabic (Egypt) ar_PS - Arabic (Palestine) ar_SA - Arabic (Saudi Arabia) bg_BG - Bulgarian bs_BA - Bosnian cs_CZ - Czech de_DE - German dk_DK - Danish el_GR - Greek en_AU - English (Australia) en_CA - English (Canada) en_GB - English (Great Britain) en_NZ - English (New Zealand) en_US - English (United States) es_ES - Spanish (Spain) es_MX - Spanish (Mexico) et_EE - Estonian fa_IR - Persian (Iran) fi_FI - Finnish fr_FR - French hi_IN - Hindi hr_HR - Croatian hu_HU - Hungarian hy_AM - Armenian it_IT - Italian ja_JP - Japanese ka_GE - Georgian (Georgia) ko_KR - Korean lt_LT - Lithuanian lv_LV - Latvian ne_NP - Nepali nl_NL - Dutch (Netherlands) no_NO - Norwegian pl_PL - Polish pt_BR - Portuguese (Brazil) pt_PT - Portuguese (Portugal) ro_RO - Romanian ru_RU - Russian sl_SI - Slovene sv_SE - Swedish tr_TR - Turkish uk_UA - Ukrainian zh_CN - Chinese (China Mainland) zh_TW - Chinese (China Taiwan)
2.3 生成个人档案信息
fake.profile(fields=None, sex=None)
结果:
2.4 批量生成人员信息
import collections import ngender import datetime import pandas as pd all_info = [] pa_list = ['name','gender', 'age', 'job', 'company', 'address', 'phone_number', 'company_email'] for i in range(15): people = collections.namedtuple('User', pa_list) people.name = fake.name() people.gender = '男' if ngender.guess(people.name)[0] == 'male' else '女' people.age = datetime.datetime.now().year - fake.date_of_birth(tzinfo=None, minimum_age=25, maximum_age=40).year # 出生日期 people.job = fake.job() people.company = fake.company() people.address = fake.address().split(' ')[0] people.phone_number = fake.phone_number() people.company_email = fake.company_email() lsts = [people.name, people.gender, people.age, people.job, people.company, people.address, people.phone_number, people.company_email] all_info.append(lsts) pd.DataFrame(all_info,columns=pa_list)
效果:
fake 实例还有很多方法可用:
- address 地址
- person 人物类
- barcode 条码类
- color 颜色类
- company 公司类
- credit_card 银行卡类
- currency 货币
- date_time 时间日期类
- file 文件类
- internet 互联网类
- job 工作
- lorem 乱数假文
- misc 杂项类
- phone_number 手机号
- python python数据
- profile 档案信息
- ssn 身份证号码
- user_agent 用户代理
官方文档:
https://faker.readthedocs.io/en/master/
END
以上就是本期为大家整理的全部内容了,赶快练习起来吧,喜欢的朋友可以点赞、点在看也可以分享让更多人知道