我们在开发中常常需要利用一些假数据来做测试,这种时候就可以使用 Faker
来伪造数据从而用来测试.
Faker 是一个可以让你生成伪造数据的Python包。当你需要初始化数据库,创建美观的XML文档,不断产生数据来进行压力测试或者想从生产服务器上拉取匿名数据的时候,Faker将是你最棒的选择。
安装
Faker库已经被提交到 pip,可以通过 pip 工具直接安装。
1
|
# pip install Faker
|
使用
faker 提供了一个工厂函数,用来创建数据。
1
2
|
from faker
import Factory
|
当然,也提供一个Faker类来创建实例
1
2
3
4
5
6
7
8
9
|
from faker
import Faker
'Audrey Robinson'
'4266 Fritz Shore\nLewischester, AL 24594-7593'
'Odio porro unde sint aliquid beatae. Ex officiis porro nostrum laboriosam deleniti nisi. A aut molestiae ratione ipsam perspiciatis facere.\nDicta incidunt at deleniti recusandae accusamus quisquam.'
|
每次调用方法 fake.name()
都会产生不同的(随机)结果。这是因为 faker 向 faker.Generator.method_name()
调用了 faker.Generator.format(method_name)
。
本地化
在用 Faker()
创建 faker 实例时,可以为实例指定本地化区域参数,默认为 ‘en_US`,因此生成的姓名、地址等都是美国的。
要生成中文伪造数据,只需:
1
|
>>> fake = Faker("zh_CN")
|
方法介绍
如上面例子,每次调用 fake 实例的 name()
方法时,都会产生不同随机姓名。fake 实例还有很多方法可用,这些方法分为以下几类:
- address 地址
- person 人物类:性别、姓名等
- barcode 条码类
- color 颜色类
- company 公司类:公司名、公司email、公司名前缀等
- credit_card 银行卡类:卡号、有效期、类型等
- currency 货币
- date_time 时间日期类:日期、年、月等
- file 文件类:文件名、文件类型、文件扩展名等
- internet 互联网类
- job 工作
- lorem 乱数假文
- misc 杂项类
- phone_number 手机号码类:手机号、运营商号段
- python python数据
- profile 人物描述信息:姓名、性别、地址、公司等
- ssn 社会安全码(身份证号码)
- user_agent 用户代理
address 地址
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 国家
fake.country()
'奥地利'
# 城市
fake.city()
'郑州市'
# 城市的后缀,中文是:市或县
fake.city_suffix()
'市'
# 地址
fake.address()
'河北省巢湖县怀柔南宁路f座 169812'
# 街道
fake.street_address()
'邯郸路W座'
# 街道名
fake.street_name()
'合肥路'
# 邮编
fake.postcode()
'314548'
# 维度
fake.latitude()
Decimal(
'68.0228435')
# 经度
fake.longitude()
Decimal(
'155.964341')
|
person 人物
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# 姓名
fake.name()
'单玉珍'
# 姓
fake.last_name()
'潘'
# 名
fake.first_name()
'琴'
# 男性姓名
fake.name_male()
'官平'
# 男性姓
fake.last_name_male()
'安'
# 男性名
fake.first_name_male()
'文'
# 女性姓名
fake.name_female()
'许颖'
|
barcode 条码
1
2
3
4
5
6
|
# 8位条码
fake.ean8()
'12771363'
# 13位条码
fake.ean13()
'9133134950963'
8)
# 自定义位数条码,只能选8或者13
fake.ean(length=
'20417161'
|
color 颜色
1
2
3
4
5
6
7
8
9
10
11
12
|
# 16进制表示的颜色
fake.hex_color()
'#671f6d'
# css用的rgb色
fake.rgb_css_color()
'rgb(237,74,237)'
# 表示rgb色的字符串
fake.rgb_color()
'208,102,218'
# 颜色名字
fake.color_name()
'Brown'
#安全16进制色
fake.safe_hex_color()
'#ee4400'
# 安全颜色名字
fake.safe_color_name()
'maroon'
|
company 公司
1
2
3
4
|
# 公司名
fake.company()
'时空盒数字科技有限公司'
# 公司名后缀
fake.company_suffix()
'科技有限公司'
|
credit_card 银行信用卡
1
2
3
4
5
6
7
8
9
10
|
None)
# 卡号
fake.credit_card_number(card_type=
'375325478746231'
None)
# 卡的提供者
fake.credit_card_provider(card_type=
'VISA 13 digit'
None)
# 卡的安全密码
fake.credit_card_security_code(card_type=
'450'
# 卡的有效期
fake.credit_card_expire()
'04/22'
None)
# 完整卡信息
fake.credit_card_full(card_type=
'Maestro\n秀芳 商\n502001016117 04/27\nCVV: 144\n'
|
currency 货币
1
2
|
# 货币代码
fake.currency_code()
'HNL'
|
date_time 时间日期
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
None)
# 随机日期时间
fake.date_time(tzinfo=
datetime.datetime(
2001,
3,
18,
17,
57,
44)
None)
# 以iso8601标准输出的日期
fake.iso8601(tzinfo=
'1973-11-16T22:58:37'
True, after_now=
False, tzinfo=
None)
# 本月的某个日期
fake.date_time_this_month(before_now=
datetime.datetime(
2017,
11,
1,
14,
33,
48)
True, after_now=
False, tzinfo=
None)
# 本年的某个日期
fake.date_time_this_year(before_now=
datetime.datetime(
2017,
3,
2,
13,
55,
31)
True, after_now=
False, tzinfo=
None)
# 本年代内的一个日期
fake.date_time_this_decade(before_now=
datetime.datetime(
2010,
3,
26,
6,
33,
23)
True, after_now=
False, tzinfo=
None)
# 本世纪一个日期
fake.date_time_this_century(before_now=
datetime.datetime(
2015,
7,
21,
19,
27,
53)
"-30y", end_date=
"now", tzinfo=
None)
# 两个时间间的一个随机时间
fake.date_time_between(start_date=
datetime.datetime(
2005,
12,
3,
17,
17,
15)
# 时区
fake.timezone()
'America/Guatemala'
"%H:%M:%S")
# 时间(可自定义格式)
fake.time(pattern=
'11:21:52'
# 随机上午下午
fake.am_pm()
'PM'
# 随机月份
fake.month()
'02'
# 随机月份名字
fake.month_name()
'August'
# 随机年
fake.year()
'1974'
# 随机星期几
fake.day_of_week()
'Sunday'
# 随机月中某一天
fake.day_of_month()
'02'
# 随机时间延迟
fake.time_delta()
datetime.timedelta(
13371,
27637)
# 随机日期对象
fake.date_object()
datetime.date(
1983,
1,
26)
# 随机时间对象
fake.time_object()
datetime.time(
17,
8,
56)
# 随机unix时间(时间戳)
fake.unix_time()
1223246848
"%Y-%m-%d")
# 随机日期(可自定义格式)
fake.date(pattern=
'1984-04-20'
None)
# 公元后随机日期
fake.date_time_ad(tzinfo=
datetime.datetime(
341,
9,
11,
8,
6,
9)
|
file 文件
1
2
3
4
5
6
7
8
|
"image", extension=
"png")
# 文件名(指定文件类型和后缀名)
fake.file_name(category=
'增加.png'
# 随机生成各类型文件
fake.file_name()
'提供.pdf'
None)
# 文件后缀
fake.file_extension(category=
'txt'
None)
# mime-type
fake.mime_type(category=
'image/png'
|
internet 互联网
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
False)
# ipv4地址
fake.ipv4(network=
'104.225.105.10'
False)
# ipv6地址
fake.ipv6(network=
'dea6:ca11:39d0:b49f:fff1:82f1:bf88:698b'
None)
# uri路径
fake.uri_path(deep=
'search/categories'
# uri扩展名
fake.uri_extension()
'.htm'
# uri
fake.uri()
'https://www.wei.com/terms/'
# url
fake.url()
'http://zheng.org/'
None, height=
None)
# 图片url
fake.image_url(width=
'https://www.lorempixel.com/700/990'
# 域名主体
fake.domain_word()
'hu'
# 域名
fake.domain_name()
'hu.cn'
# 域名后缀
fake.tld()
'com'
# 用户名
fake.user_name()
'xia13'
# UA
fake.user_agent()
'Opera/8.33.(Windows NT 5.1; an-ES) Presto/2.9.171 Version/10.00'
# MAC地址
fake.mac_address()
'd6:38:cc:2a:76:b2'
# 安全邮箱
fake.safe_email()
'mingli@example.net'
# 免费邮箱
fake.free_email()
'tao44@gmail.com'
# 公司邮箱
fake.company_email()
'jingzhong@wang.cn'
# 邮箱
fake.email()
'changjun@hao.com'
|
job 工作
1
2
3
4
|
#工作职位
fake.job()
'Dealer'
'Musician'
|
lorem 乱数假文
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
200)
# 随机生成一篇文章
fake.text(max_nb_chars=
'语言无法应用为什一点国内.要求完成如何世界电脑发布作品.经济不同教育个人科技全国.\n在线学生发布信息上海状态.\n联系一次通过其实介绍世界.增加也是使用成功那个.\n商品免费管理公司.留言自己这种内容.\n次数内容知道这样女人感觉.操作他的生产出现如何报告文章只有.\n个人文化中心不能发布最新.质量一下提高.感觉最大工具表示最后计划.这是还有次数结果其实特别.'
# 随机单词
fake.word()
'能力'
3)
# 随机生成几个字
fake.words(nb=
[
'国家',
'经营',
'结果']
6, variable_nb_words=
True)
# 随机生成一个句子
fake.sentence(nb_words=
'重要更多我们作品地方增加.'
3)
# 随机生成几个句子
fake.sentences(nb=
[
'制作上海学生.',
'方式汽车一样技术帮助欢迎.',
'说明一种深圳经营电话帖子.']
3, variable_nb_sentences=
True)
# 随机生成一段文字(字符串)
fake.paragraph(nb_sentences=
'非常环境位置有限发展首页行业.情况对于出现部门这种觉得.产品以后因为虽然由于日本不同.'
3)
# 随机生成成几段文字(列表)
fake.paragraphs(nb=
[
'就是发布要求有关这里国际.美国设备深圳经营.首页也是支持报告.',
'决定可是只有发现开始一直.最后有些项目正在深圳关系决定.下载注册图片更多进行他的那些.',
'必须他们发生数据准备联系.同时这样内容学校精华.']
|
misc 杂项
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
10)
# 随机二进制字符串(可指定长度)
fake.binary(length=
b'U\xa9@\x1e\x96\xe7\xca\x82\x14f'
# 随机语言代码
fake.language_code()
'tg'
False)
# 随机md5,16进制字符串
fake.md5(raw_output=
'cc4feebe419791332bbcff5e0fdf084a'
False)
# 随机sha1,16进制字符串
fake.sha1(raw_output=
'8ac0e9980f880860b6e45ae6fd257cc847b7ae8d'
False)
# 随机sha256,16进制字符串
fake.sha256(raw_output=
'033151f173f4a389e38e7df2363d89741f752c474e7bdfa2ee0a794bf0b505b5'
50)
# 随机真假值(得到True的几率是50%)
fake.boolean(chance_of_getting_true=
False
# 随机真假值和null
fake.null_boolean()
True
10, special_chars=
True, digits=
True, upper_case=
True, lower_case=
True)
# 随机密码(可指定密码策略)
fake.password(length=
'F%722TJg_U'
# 随机本地代码
fake.locale()
'hy_AM'
# 随机uuid
fake.uuid4()
'a50d17e7-bc4f-37a3-27b3-04a24fdd0055'
>>>
|
phone_number 电话号码
1
2
3
4
|
# 手机号码
fake.phone_number()
'13334603608'
# 运营商号段,手机号码前三位
fake.phonenumber_prefix()
158
|
python python数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# 随机int
fake.pyint()
7775
None, right_digits=
None, positive=
False)
# 浮点数
fake.pyfloat(left_digits=
-84901.5586333
None, right_digits=
None, positive=
False)
# 随机高精度数
fake.pydecimal(left_digits=
Decimal(
'-12273687068527.0')
None, max_chars=
20)
# 随机字符串(可指定长度)
fake.pystr(min_chars=
'cblutNKFIyegfcHPrjzx'
# 随机bool值
fake.pybool()
True
10, variable_nb_elements=
True)
# 随机iterable
fake.pyiterable(nb_elements=
[
'ODfeVvcbAjPDBGwzljQw',
'https://www.tan.cn/list/category/homepage.php',
'YQlrsFkBieyKYaXlCljJ', Decimal(
'42778240911787.2'), Decimal(
'957411812.6383'),
'TGbqZufoiUXLQTZDrVcP',
'http://yan.com/posts/tags/search/terms.php',
3.680492634254,
'min57@hotmail.com', datetime.datetime(
2001,
8,
16,
6,
10,
49),
'xMMOjlETIgKGqVGTrChG',
'yong83@xu.cn']
10, variable_nb_elements=
True )
# 随机生成一个list
fake.pylist(nb_elements=
[
'KXQMXAkcEMSLfnIZkgJb',
'BtowiRsuIqyyULnSYYdr', datetime.datetime(
2011,
10,
10,
14,
44,
2), datetime.datetime(
2008,
5,
10,
1,
38,
38),
'juan47@hotmail.com',
'QEsdUpEqHLpThyWCjkNx', Decimal(
'-801375867.9'),
'ucDyeZnHAXfZtkwdVUbR',
4707, datetime.datetime(
1974,
8,
7,
1,
54,
29)]
10, variable_nb_elements=
True)
# 随机字典
fake.pydict(nb_elements=
{
'其中':
9047,
'一直':
'AUiUjuqccIdVAWSqzDbW',
'选择':
'ddong@hotmail.com',
'开发': datetime.datetime(
1972,
10,
20,
14,
14,
9),
'电影':
'KYmolBhkjSRxloXXFUUT',
'文化':
2681,
'这里':
'uyang@yahoo.com',
'不会':
'ZPkwuxWsrJSHMNuFiWEx',
'社会':
'CiujeaZMZSuyYwuKzEdN'}
10, variable_nb_elements=
True)
# 随机set
fake.pyset(nb_elements=
{
'bhe@hotmail.com',
'http://fu.cn/list/home.htm',
'MlJluVirRkofBnKNtphM',
296,
'ghoUSHkuEGmCzlJFKyHZ', datetime.datetime(
2008,
4,
4,
2,
55,
4),
'AgbynHjdvwYpUkbMsfqr',
8751,
9649,
'tangguiying@hotmail.com', Decimal(
'5727570036.91'),
'HmDkExndcQIOaTtsSpsc',
'hjQlLLXuHVVzENEwoHJK'}
10, variable_nb_elements=
True)
# 随机tuple
fake.pytuple(nb_elements=
(
'http://www.cai.com/index/', datetime.datetime(
1973,
7,
28,
2,
12,
23),
'khltJQMYJvIDRMYodviZ',
'uJezUsEqiHaiFxwOPWvl',
'qojwZHyytBSQQavkDaTu',
'AHUCHYuVJTHnoSEuQDSY',
1012,
'uEYVuzeTlgVhrnCATfKw',
'https://www.zhou.com/categories/tags/main/',
'LbLSFZPeATtzHvbmYhGr')
# 随机生成3个有10个元素的python数据结构
fake.pystruct()
([datetime.datetime(
1996,
10,
26,
7,
35,
26), datetime.datetime(
1998,
2,
28,
17,
20,
8),
'qianming@hotmail.com',
'yEWMrpTqtAHfbxqldGrb',
'YgKYOnrjuthOrOXhlYIl', datetime.datetime(
1994,
12,
10,
0,
55,
30), Decimal(
'-6865068.3'),
'SYHFHiFvJlRVPcCKumUM',
-8619.4354,
'kwGipwcASeALLeKdaWBi'], {
'同时':
'BvtYdkNTHwZNMiIIRwKd',
'空间':
310959668662.457,
'特别':
'PZQDBuuQWkcdryMloyKS',
'音乐': Decimal(
'-7219015925.0'),
'项目':
'https://www.zhou.cn/main.php',
'回复':
30.408750841,
'显示':
'etZMrsjXJgZpDfZWhpoS',
'大小':
7472,
'类型':
'OsjpxgLqnTcdVOlHoMoP',
'什么':
'http://www.xia.com/posts/app/index/'}, {
'朋友': {
0:
-415025.243093017,
1: [
1799,
585,
'shu@hotmail.com'],
2: {
0:
9980,
1:
'qnOnFTzGnsjvXGybBnMF',
2: [
'http://zheng.cn/list/search/tag/faq.html', Decimal(
'964285276661463.0')]}},
'发生': {
1:
-5339010.6134,
2: [datetime.datetime(
1987,
6,
23,
15,
21,
45),
'SvSpvKqTXlJvdQhHScwM',
'PZjKOYzZzoNVsHLRcARQ'],
3: {
1:
'https://www.yan.cn/register/',
2:
2191,
3: [
'HMHKQuLRBQaaAypRbtHU', datetime.datetime(
2014,
5,
24,
3,
32,
36)]}},
'我的': {
2:
'linxia@yahoo.com',
3: [
'http://gu.com/about.php',
'DhzpWYkgLCobGSHDLXzI',
1420],
4: {
2: datetime.datetime(
1986,
2,
22,
4,
50,
12),
3:
'rRHwQQzkpAMBQxwVITBa',
4: [datetime.datetime(
1984,
2,
6,
11,
52,
18),
-73821572962388.7]}},
'全部': {
3:
'uMQeeBXYNGhrHnAerdjp',
4: [
323329.56403,
'kFvqXFhhtQPNtrOjKtxa',
'http://peng.cn/'],
5: {
3:
'lcaoeisYIAOsuRjbOXia',
4: Decimal(
'-902407032449085.0'),
5: [
2296,
'uyuWgnsONzLluXqXdASM']}},
'大小': {
4:
'min63@hotmail.com',
5: [datetime.datetime(
1991,
10,
22,
19,
21,
48),
'cDYEScdIokWuvGhRkWqs',
'XeDJojWyywFvzmWYaokO'],
6: {
4: Decimal(
'-4167029.2915827'),
5:
4030,
6: [
6372,
'xfRXXEFGsNQpeIGmbaHU']}},
'语言': {
5:
'https://www.guo.cn/',
6: [
6498, datetime.datetime(
1981,
12,
3,
18,
4,
29),
42598100345.61],
7: {
5:
'http://zheng.cn/',
6:
-229316.268238,
7: [
'https://www.qiao.cn/home/',
'GtaEXeVxjRnnkggjEguv']}},
'表示': {
6:
'xcBvcGUWxdMhDqgzmhSd',
7: [
'kwDaFhyTKqHajGSaNOMf',
8561,
5456],
8: {
6:
'https://zhou.cn/tags/tag/faq.html',
7:
'IGayAZtTQVlSOasQwgug',
8: [
'iCjwBzHfmPSLqAgmIOle',
70511766106574.5]}},
'电子': {
7:
'http://www.lu.org/tags/posts/terms/',
8: [datetime.datetime(
1978,
8,
11,
12,
16,
35),
'https://song.cn/main/categories/homepage.html',
'https://long.com/register.html'],
9: {
7:
'nFJNhyMYBvfTxrYwYPUQ',
8:
-2252757903.0,
9: [
'GuJnhMEgXoMAivrgGZie', datetime.datetime(
2014,
5,
29,
1,
17,
50)]}},
'的人': {
8:
'WgHePwYSPgSiPllXpLlJ',
9: [
1782,
'fSepbXwpvhiBphzDTDNC',
-9117731.63459416],
10: {
8:
5602,
9:
3664,
10: [
'http://www.mao.com/search/tag/main/', Decimal(
'5579.7377')]}},
'不过': {
9:
'rOfkDPTHvzKbfvQHbPNm',
10: [
314.397,
'StqeLyXkIDKHfExSjggk',
'xiuyingkang@gao.cn'],
11: {
9:
'BLKNdcccamYzBwRcMxlx',
10:
'yongduan@gong.org',
11: [
'ZocoQHdbhaNloWALnzwt',
'eWVvvHurAlZZRxlYHZXi']}}})
|
profile 人物描述信息
1
2
3
4
5
6
7
8
9
10
11
12
|
None, sex=
None)
# 人物描述信息:姓名、性别、地址、公司等
fake.profile(fields=
{
'job':
'Licensed conveyancer',
'company':
'万迅电脑信息有限公司',
'ssn':
'370684199902182726',
'residence':
'福建省小红市南长广州街K座 406448',
'current_location': (Decimal(
'18.050895'), Decimal(
'-0.877117')),
'blood_group':
'0-',
'website': [
'https://www.yi.org/',
'https://www.hu.com/',
'https://www.yin.cn/'],
'username':
'minghuang',
'name':
'后英',
'sex':
'F',
'address':
'安徽省秀荣市璧山嘉禾路T座 954960',
'mail':
'czhong@hotmail.com',
'birthdate':
'1975-03-09'}
"m")
# 人物精简信息
s = fake.simple_profile(sex=
for i,v
in s.items():
...
username chao85
name 邴宇
sex M
address 陕西省东市朝阳廖街Y座
757661
mail xiazhang@gmail.com
birthdate
1996
-09
-20
|
ssn 社会安全码(身份证)
1
2
3
4
|
# 随机生成身份证号(18位)
fake.ssn()
'140100196612297997'
18
|
user_agent 用户代理
常用在伪造浏览器信息
1
2
|
# 伪造UA
fake.user_agent()
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/5361 (KHTML, like Gecko) Chrome/15.0.812.0 Safari/5361'
|
平台信息伪造
1
2
3
4
5
6
7
8
9
10
|
'X11; Linux i686'
'i686'
'Windows CE'
'Macintosh; Intel Mac OS X 10_7_4'
'PPC'
|
浏览器伪造
1
2
3
4
5
6
7
8
9
10
|
# IE浏览器
fake.internet_explorer()
'Mozilla/5.0 (compatible; MSIE 5.0; Windows NT 6.1; Trident/4.0)'
# opera浏览器
fake.opera()
'Opera/9.37.(Windows 95; doi-IN) Presto/2.9.178 Version/10.00'
# firefox浏览器
fake.firefox()
'Mozilla/5.0 (Windows NT 5.0; te-IN; rv:1.9.2.20) Gecko/2015-09-28 13:29:05 Firefox/12.0'
# safari浏览器
fake.safari()
'Mozilla/5.0 (Windows; U; Windows NT 4.0) AppleWebKit/533.37.4 (KHTML, like Gecko) Version/5.0 Safari/533.37.4'
# chrome浏览器
fake.chrome()
'Mozilla/5.0 (Windows 98; Win 9x 4.90) AppleWebKit/5361 (KHTML, like Gecko) Chrome/14.0.866.0 Safari/5361'
|
自定义扩展
Faker 已经提供了足够丰富的信息生成,包括名字、手机号、邮箱地址、邮编等等。尽管如此,可能还是没有办法满足你的需求。这时,可以利用自定义扩展,引用外部的 provider,自定义你要的功能。
Faker 对象可以通过 add_provider
方法将自定义的 Provider 添加到对象中,自定义的 Provider 需要继承自 BaseProvider。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
from faker
import Faker
fake = Faker()
# first, import a similar Provider or use the default one
from faker.providers
import BaseProvider
# create new provider class
class MyProvider(BaseProvider):
def foo(self):
return
'bar'
# then add new provider to faker instance
fake.add_provider(MyProvider)
# now you can use:
print(fake.foo())
|
结果显示:
1
|
bar
|
随机控制
Faker
随机生成由 random.Random
驱动。其中,.random
属性返回 random.Random
对象。通过对该对象的操作,可以实现自定义的行为。
1
2
3
4
|
from faker
import Faker
fake = Faker()
fake.random
fake.random.getstate()
|
那么,可以实现什么自定义呢?举个例子,我们可以设置 seed
,通过给定的 seed
可以控制每次生成的内容都是一样的。
1
2
3
4
5
6
7
8
9
10
11
12
|
from faker
import Faker
4321)
fake.random.seed(
'Ryan Gallagher'
'7631 Johnson Village Suite 690\nAdamsbury, NC 50008'
4321)
fake.random.seed(
'Ryan Gallagher'
'7631 Johnson Village Suite 690\nAdamsbury, NC 50008'
|
不同的两次运行,只要seed一样,生成出来的信息就是一样的。
注意:fake.random.seed(4321)
可以写成 fake.seed(4321)
。
命令行生成
有时想在shell或者其他程序中生成一些伪数据,是不是一定要写一个Python脚本呢?别急——Faker提供了一个命令行工具,估计可以应对大部分场景了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
$ faker address
968 Bahringer Garden Apt. 722Kristinaland, NJ 09890
$ python3 -m faker address
432 Marvin Wells Apt. 593\nWest Eric, DC 45650-8420
$ faker -l de_DE address
Samira-Niemeier-Allee 5694812 Biedenkopf
$ faker profile
{
'job':
'Designer, blown glass/stained glass',
'company':
'Dennis-Bowers',
'ssn':
'034-28-9965',
'residence':
'34796 Jeremiah Station Apt. 782\nWest Timothy, TX 24139-6974',
'current_location': (Decimal(
'-47.425017'), Decimal(
'-42.743615')),
'blood_group':
'0+',
'website': [
'https://www.gardner.biz/',
'http://glover-ellison.info/',
'http://www.harrison.biz/'],
'username':
'patrick33',
'name':
'Alexandra Montgomery',
'sex':
'F',
'address':
'2314 Collier Stream Suite 093\nMcintyreside, UT 19553',
'mail':
'gomezterri@hotmail.com',
'birthdate':
'2005-01-30'}
$ faker profile ssn,name
{
'ssn':
'344-68-7420',
'name':
'Veronica Brennan'}
$ faker -r=3 -s=
";" name
Willam Kertzmann;
Josiah Maggio;
Gayla Schmitt;
|
faker 命令帮助:
1
2
3
4
5
|
faker [-h] [--version] [-o output]
[-l {bg_BG,cs_CZ,...,zh_CN,zh_TW}]
[-r REPEAT] [-s SEP]
[-i {module.containing.custom_provider othermodule.containing.custom_provider}]
[fake] [fake argument [fake argument ...]]
|
选项说明:
faker
: 在shell中,faker
命令也可以用python -m faker
来代替-h
,--help
: 帮助信息--version
:显示版本-o FILENAM
:输出结果到文件中-l {bg_BG,cs_CZ,...,zh_CN,zh_TW}
:指定本地化,zh_CN
表示中文-r REPEAT
:指定生成多少条相同类型的数据-s SEP
:在每个输出后边添加指定的分隔符-i {my.custom_provider other.custom_provider}
:自定义扩展,prividers
列表。注意,这里要指定包含你provider
类的模块的路径,而不是程序本身。fake
:指定方法名称,如:name
,address
,text
等[fake argument ...]
:为方法指定参数。如上例,为profile
方法指定ssn
和name
参数,只输出这两个类型的内容。