睁眼说瞎话并不为程序员所擅长,大多码农都避之不及,可在编程过程中,出于测试或是临时提供接口的目的,我们总得制造伪数据。如果需要伪造人名,三个以内还好,张三李四王五,可超过10个的话,我不知道你是不是就要侵犯你好朋友的姓名权了

而如果是

1
2
for in range(1,100):
    Student(name,address,phone_number)

你要怎么办呢?

#faker闪亮登场 ##Install faker pip install fake-factory

##Usage 拿上边的问题来说,我们需要产生一组(name,address,phone_number)

1
2
3
4
5
from faker import Factory
fake = Factory.create('zh_CN')
for in range(1,100): 
    (name,address,phone_number) = (fake.name(),fake.address(),fake.phone_number())
    print name,address,phone_number #Student(name,address,phone_number)

输出为

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
:::text
危莉 超市皇路u座 109784 18689765254
富东 琴市东街Q座 532875 15549156990
白宇 帆市闵街Y座 106094 14580766320
俞玉英 坤市督街k座 509819 13771875966
陆淑珍 娟市乐路X座 113166 15929147106
束红梅 磊市贲街h座 699061 15315800191
牧秀云 杰市滕街l座 315704 14564550529
游平 敏市牧街D座 152585 18580747599
万雷 桂花市屈路t座 423929 15649801854
司华 慧市索街b座 208656 13748193777
尹华 春梅市荀街d座 834796 13621273546
赖桂兰 丽丽市白街Z座 759194 18948910936
卜勇 秀珍市冷路r座 589146 15565418368
终婷婷 军市丁路v座 724209 14720124023
东芳 桂英市谭街Z座 570071 18558694020
羊红霞 丹市柴街q座 806049 13153710279
芮倩 琴市席街p座 291743 15030602445
阮淑珍 丽丽市滕路q座 911886 18900809224
丁芳 冬梅市佴路V座 424657 13332629139
车畅 玉珍市蓝路v座 549485 15826010349

你看睁眼说瞎话容易多了吧!


例子过后,下边我们来正式介绍下faker

#What

Faker is a Python package that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.

少量数据我们还可以自己瞎编,一旦数据变多,faker的好处就体现出来了,这对自动化很有好处

#使用演示

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
fake.name()
# 'Lucy Cechtelar'

fake.address()
# "426 Jordy Lodge
#  Cartwrightshire, SC 88120-6700"

fake.text()
# Sint velit eveniet. Rerum atque repellat voluptatem quia rerum. Numquam excepturi
# beatae sint laudantium consequatur. Magni occaecati itaque sint et sit tempore. Nesciunt
# amet quidem. Iusto deleniti cum autem ad quia aperiam.
# A consectetur quos aliquam. In iste aliquid et aut similique suscipit. Consequatur qui
# quaerat iste minus hic expedita. Consequuntur error magni et laboriosam. Aut aspernatur
# voluptatem sit aliquam. Dolores voluptatum est.
# Aut molestias et maxime. Fugit autem facilis quos vero. Eius quibusdam possimus est.
# Ea quaerat et quisquam. Deleniti sunt quam. Adipisci consequatur id in occaecati.
# Et sint et. Ut ducimus quod nemo ab voluptatum.

#中文本地化相关 locales/zh_CN

#参考