机器都在不停学习
人怎么能停止脚步

用Python生成假数据

往数据库中插入数据的时候,经常需要造很多的假数据.比如用户名,省份,城市,文本信息等

Python有一个好用的包Faker,可以非常迅速地帮助完成这类任务.

安装方法:

pip install Faker

示例代码:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from faker import Faker
fake = Faker()

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.

也可以这样快速批量生成数据

for _ in range(10):
  print(fake.name())

这里生成的是英文的假数据,实际上在fake=Fake()的括号中,可以指定语言.


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
ar_EG - Arabic (Egypt)
ar_PS - Arabic (Palestine)
ar_SA - Arabic (Saudi Arabia)
bg_BG - Bulgarian
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
it_IT - Italian
ja_JP - Japanese
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)
zh_TW - Chinese (Taiwan)
ka_GE - Georgian (Georgia)

我们将地区切换回中国,可以看到这里提供了丰富的数据类型.


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
45
46
47
48
49
50
from faker import Faker

fake = Faker("zh_CN")
fake.city_suffix()
# '市'

fake.building_number()
# 'U座'

fake.city()
# '兴城县'

fake.country_code(representation="alpha-2")
# 'BZ'

fake.country()
# '安道尔'

fake.address()
# '四川省畅县丰都太原街b座 122990'

fake.geo_coordinate(center=None, radius=0.001)
# Decimal('-166.076758')

fake.street_suffix()
# '街'

fake.latitude()
# Decimal('-5.1430775')

fake.street_address()
# '北京街d座'

fake.district()
# '城东'

fake.postcode()
# '822070'

fake.province()
# '黑龍江省'

fake.longitude()
# Decimal('126.744241')

fake.city_name()
# '汕尾'

fake.street_name()
# '兴城路'

关于Faker包,更详细的用法可以参考官方文档以及github

赞(0)
转载请注明出处机器在学习 » 用Python生成假数据
分享到: 更多 (0)

评论 抢沙发

Scroll Up