Faker简介
今天要介绍的是一个Python的工具类库Faker。没错就是Faker,但是不是大飞老师。
在软件开发中,经常需要使用一些测试数据。对这种情况,我们一般要么使用已有的系统数据,要么需要手动制造一些数据。在手动制造数据的过程中,可能需要花费大量精力和工作量,而使用faker生成虚拟数据可以为我们减少这部分的工作量
安装
pip install faker
引用和初始化
引用
import faker
初始化
fake = faker.Faker()
#或者指定location 为中文
fake = faker.Faker('zh')
faker支持了很多的语言类型,可以通过faker命令行帮助文档查询
faker -help
常用的方法
>>> fake.name() # 生成姓名
'田鑫'
>>> fake.address() # 生成地址
'山东省阳市高坪吕路A座 998657'
>>> fake.country() # 国家
'意大利'
>>> fake.province() # 省份
'安徽省'
>>> fake.city() # 城市
'哈尔滨市'
>>> fake.district() # 区
'徐汇'
>>> fake.street_address() # 街道
'海门路I座'
>>> fake.random_int() # 随机数字,默认0~9999
2257
>>> fake.random_digit() # 0~9随机数
6
>>> fake.random_number() # 随机数字,参数digits设置生成的数字位数,返回random.randint(0, pow(10, digits) - 1)
3229
>>> fake.random_letter() # 随机字母
'Q'
>>> fake.random_lowercase_letter() # 随机小写字母
'z'
>>> fake.random_uppercase_letter() # 随机大写字母
'V'
>>> fake.color_name() # 颜色名
'GoldenRod'
>>> fake.color_name()
'Chartreuse'
>>> fake.color_name()
'DeepPink'
>>> fake.color_name()
'MediumSpringGreen'
>>> fake.company() # 随机公司名
'联通时科网络有限公司'
>>> fake.bs() # 随机公司服务名
'mesh bleeding-edge infrastructures'
>>> fake.company_suffix() # 随机公司性质
'信息有限公司'
>>> fake.credit_card_number() # 信用卡号
'4803099375057291529'
>>> fake.credit_card_provider() # 信用卡类型
'VISA 19 digit'
>>> fake.currency_code() # 货币代码
'EUR'
>>> fake.am_pm() # AM/PM
'AM'
>>> fake.date() # 日期
'1974-08-12'
>>> fake.date_this_year() # 今年的随机日期
datetime.date(2018, 5, 6)
>>> fake.date_this_month() # 这个月的随机日期
datetime.date(2018, 11, 17)
>>> fake.month() # 随机月份数字
'09'
>>> fake.month_name() # 随机月份名称
'July'
>>> fake.date_time_this_year() # 今年的某个时间
datetime.datetime(2018, 7, 21, 7, 43, 58)
>>> fake.date_time() # 随机时间
datetime.datetime(2007, 9, 13, 14, 15, 54)
>>> fake.time() # 随机24小时时间,time对象
'23:28:47'
>>> fake.file_name() # 文件名
'更新.html'
>>> fake.file_path() # 文件路径
'/的话/系列.docx'
>>> fake.file_extension() # 文件扩展
'xlsx'
>>> fake.mime_type() # 随机mime类型
'video/ogg'
>>> fake.ascii_company_email() # 随机公司邮箱
'guiying74@xiajun.net'
>>> fake.ascii_email() # 随机邮箱
'tangyan@gmail.com'
>>> fake.ipv4() # 随机IP4地址
'126.162.176.179'
>>> fake.ipv6() # 随机IP6地址
'9be4:c8c9:f589:f14b:24e6:2425:88c:bef9'
>>> fake.mac_address() # 随机MAC地址
'7e:51:97:aa:8b:a1'
>>> fake.url() # 随机URI地址
'http://luo.cn/'
>>> fake.job() # 随机职位
'网络工程师'
>>> fake.paragraph() # 段落
'准备帮助标题论坛.朋友开始类型网上这种.日本其他然后城市.'
>>> fake.sentence() # 随机一句话
'产品应用操作详细.'
>>> fake.word() # 单词
'参加'
>>> fake.boolean() # 随机布尔值
False
>>> fake.phone_number() # 随机手机号
'18071087230'
>>> fake.profile() # 随机档案
{'job': '银行柜员', 'company': '四通科技有限公司', 'ssn': '220200194905157548', 'residence': '山西省长沙市城东童街R座 486365', 'current_location': (Decimal('61.941104'), Decimal('-177.651444')), 'blood_group': 'A+', 'website': ['https://gong.cn/', 'https://www.xiuyingna.org/', 'http://xp.cn/', 'http://www.wei.org/'], 'username': 'wei07', 'name': '廉雪梅', 'sex': 'F', 'address': '山西省金凤市上街公路M座 409920', 'mail': 'taotian@gmail.com', 'birthdate': datetime.date(1911, 12, 12)}
>>> fake.ssn() # 身份证号
'510726199311249157'
>>> fake.firefox() # 随机生成FireFox的浏览器user_agent信息
'Mozilla/5.0 (X11; Linux x86_64; rv:1.9.6.20) Gecko/2013-12-19 08:38:18 Firefox/13.0'
>>> fake.user_agent() # 随机user_agent信息
'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_1 like Mac OS X; ca-AD) AppleWebKit/531.14.4 (KHTML, like Gecko) Version/3.0.5 Mobile/8B113 Safari/6531.14.4'