請啟用JavaScript來查看內容

Python 使用 Faker 套件來生成假資料

前言

有時候可能因為某些原因需要生成一些假資料,例如做網頁時,先填入一些假資料方便排版。

在 Python 中就有一個 Faker 套件來產生假資料,包括人名、電話、地址、工作、公司、信用卡、IP……非常多樣。

安裝

安裝也很簡單,可以透過 pip 安裝:

1
pip install faker

這邊要注意的是從版本 v4.0.0 開始,Faker 只支援 Python 3.5 及更新版本,並不支援 Python 2。

以下以撰寫文章時最新的版本 v4.14.2 為範例。
Faker 官方GitHub
Faker 官方文件

簡易範例

使用 faker.Faker() 來創建並初始化產生器,調用方法來產生資料,例如以下名字、地址。

1
2
3
4
5
6
7
8
9
from faker import Faker

fake = Faker()

fake.name()
# Carla Parker

fake.address()
# 1882 Erika Glens\nRoblesbury, MO 28877

語言包

看到這裡,可能有人覺得都是生成英文數據,我想要繁體中文那怎麼辦🤔
別擔心,Faker 有內建語言包的機制,在一開始創建 Faker 時帶入語言參數即可。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
fake = Faker('zh_TW')

for _ in range(5):
    print(fake.name())
    print(fake.address())
    print('--------')

# 許哲瑋
# 45851 臺東市復興路11號之9
# --------
# 林雅慧
# 67491 台北縣學府巷468號之1
# --------
# 陳宇軒
# 20610 新竹民治巷862號之2
# --------
# 殷沖
# 80604 橫山中興路5段595號5樓
# --------
# 李信宏
# 51142 台北大同路172號之2
# --------

你如果想要多個語言混合,當然也行🤣

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fake = Faker(['zh_TW', 'ja_JP', 'ko_KR'])

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

# 佐々木 明美
# 高橋 直子
# 宋惠如
# 이영일
# 佐々木 桃子
# 김미영

一些常見的語言代碼如下:

* 題外話:我發現它的繁體中文有些地方不是很好,感覺是從簡體直接轉過來的。

方法介紹

人名

與人名相關數據,例如:姓氏、名字、羅馬拼音等等。

 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
print(fake.name())
# 袁家銘
print(fake.name_female())  # 女生人名
# 莫怡萱
print(fake.name_male())    # 男生人名
# 孫威廷
print(fake.romanized_name())  # 羅馬拼音
# Ping Long

print(fake.last_name())
# 楊
print(fake.last_name_female())
# 李
print(fake.last_name_male())
# 黃
print(fake.last_romanized_name())
# Lin

print(fake.first_name())
# 家豪
print(fake.first_name_female())
# 惠雯
print(fake.first_name_male())
# 冠霖
print(fake.first_romanized_name())
# Jing

個人資料

像是:語言、手機號碼、身分證、工作職稱。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
print(fake.language_name())
# Turkmen
print(fake.country_calling_code())
# +290 8
print(fake.phone_number())
# 0977-739186
print(fake.ssn())
# D169226625
print(fake.job())
# 工業設計

print(fake.simple_profile())
# {'username': 'sfang', 'name': '李雅文', 'sex': 'M', 'address': '42346 板橋縣民有路310號之3', 'mail': 'xiuyingxue@gmail.com', 'birthdate': datetime.date(1936, 2, 13)}

地址

與地址相關數據,例如:完整地址、城市、鄉鎮、路名……。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
print(fake.address())
# 30751 基隆市仁愛街93號6樓
print(fake.building_number())
# 4號
print(fake.city())
# 卑南縣
print(fake.city_name())
# 草屯
print(fake.city_name_suffix())
# 縣
print(fake.country())
# 俄羅斯
print(fake.country_code())
# LT
print(fake.postcode())
# 824
print(fake.secondary_address())
# 之2
print(fake.street_address())
# 自由街759號
print(fake.street_name())
# 信義
print(fake.street_name_suffix())
# 路

公司

與公司相關數據,例如:公司名稱、公司前綴、公司後綴等等。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
print(fake.bs())
# synergize enterprise relationships
print(fake.catch_phrase())
# Object-based zero administration secured line
print(fake.company())
# 台北眾大捷運股份有限公司
print(fake.company_prefix())
# 品誠
print(fake.company_suffix())
# 有限公司

句子

與句子、單詞相關數據。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
print(fake.sentence())
# 發生服務選擇人民游戲然后一下法律.
print(fake.sentences())
# ['發現密碼地區那個精華制作.', '擁有地方手機所有.', '業務決定地址經濟投資方式.']
print(fake.text(max_nb_chars=15))
# 基本主要研究根據一種當然各種.
print(fake.word())
# 關於
print(fake.words())
# ['一種', '電影', '大學']

日期時間

日期時間相關數據,譬如:日期、時間、星期……。

 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
print(fake.am_pm())
# AM
print(fake.iso8601())
# 1988-07-01T02:40:03

print(fake.date())
# 1970-12-22
print(fake.date_object())
# datetime.date(2004, 3, 20)

print(fake.date_this_month(before_today=True, after_today=False))
# 2020-11-07
print(fake.day_of_month())
# 01
print(fake.day_of_week())
# Saturday
print(fake.month_name())
# April

print(fake.time())
# 02:46:38
print(fake.time_object())
# datetime.time(8, 43, 22)
print(fake.unix_time())
# 1429693221

檔案

包括檔案名稱、附檔名、路徑等等。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
fake = Faker()

print(fake.file_extension(category='image'))
# jpeg
print(fake.file_name())
# central.png
print(fake.file_path())
# /recently/pass.js
print(fake.unix_device())
# /dev/sdg

網路

與網路相關數據,例如:Email、HTTP方法、IP、網址、網域……。

 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
print(fake.company_email())
# dmeng@zou.com
print(fake.domain_name())
# tang.com
print(fake.domain_word())
# gao
print(fake.email())
# slei@gmail.com
print(fake.free_email())
# mliu@hotmail.com
print(fake.free_email_domain())
# yahoo.com
print(fake.hostname())
# desktop-15.wan.com
print(fake.http_method())
# POST
print(fake.image_url())
# https://placeimg.com/431/555/any
print(fake.ipv4())
# 28.144.124.103
print(fake.ipv4_network_class())
# c
print(fake.ipv4_private())
# 192.168.96.99
print(fake.ipv4_public())
# 75.81.186.65
print(fake.mac_address())
# d2:3c:7b:3b:93:4f
print(fake.uri())
# http://fu.com/wp-content/categories/main/index/
print(fake.uri_extension())
# .php
print(fake.uri_page())
# search
print(fake.url())
# http://qiao.com/
print(fake.user_name())
# mingyan

顏色

生成顏色色碼,像是:HEX、RGB、HSV、HSL等等。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
print(fake.color())
# #6626ef
print(fake.color(hue='red'))
# #ed6595
print(fake.color(hue=(100, 200), color_format='rgb'))
# rgb(36, 181, 79)
print(fake.color(hue=135, luminosity='dark', color_format='hsv'))
# hsv(135, 95, 65)
print(fake.color_name())
# DarkMagenta
print(fake.hex_color())
# #dd5cbb
print(fake.rgb_color())
# 35,232,144

信用卡

信用卡相關資訊。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
print(fake.credit_card_full())  # 信用卡詳細信息
# JCB 16 digit
# 鈺婷 柳
# 3580134175667608 07/21
# CVC: 070

print(fake.credit_card_number())  # 卡號
# 377183544094989
print(fake.credit_card_expire())  # 有效日期
# 02/27
print(fake.credit_card_provider())  # 提供商名稱
# VISA 19 digit
print(fake.credit_card_security_code())  # 安全碼
# 320

Python 相關

與 Python 相關資料格式,可能對於測試很好用(?),例如:Email、HTTP方法、IP、網址、網域……。
(竟然還有這種東西😮)

1
2
3
4
5
6
7
8
print(fake.pybool())
# False
print(fake.pydecimal())
# -7867266828.6
print(fake.pydict())
# {'都是': 'MyXkRCJjYanEWNNuYrLM', '一次': 'qiang69@gmail.com', '政府': Decimal('2879320.4'), '工作': 'fDURUWoCzsGhHdvjJHXM', '信息': 1080, '功能': 'hNvGqVhrMVwOYiYpYghC', '要求': Decimal('41.93441406219'), '一切': 'https://mao.tw/homepage/', '開始': 446798.631125}
print(fake.pylist())
# ['IartORkSBcHGqJCpfbMq', 'https://www.cui.com/terms.htm', datetime.datetime(1995, 12, 12, 4, 32, 50), 'vsJNgfvnxjDttefVvoIu', 'JxzLdZhLDYvgZHiHWjdO', 6960, 'mojun@hotmail.com', 'liangchao@yahoo.com', datetime.datetime(2013, 5, 9, 12, 38, 18), 'phao@hotmail.com', 'https://mo.com/privacy/', 6105]

其他更多

 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
print(fake.random_digit())
# 8
print(fake.random_digit_not_null())
# 3
print(fake.random_int(min=0, max=15))
# 12
print(fake.random_element(elements=('a', 'b', 'c', 'd')))
# d

print(fake.numerify(text='Intel Core i%-%%##K vs AMD Ryzen % %%##X'))
# Intel Core i8-9363K vs AMD Ryzen 4 6134X

print(fake.language_code())  # i18n 語言碼
# uk
print(fake.license_plate())  # 車牌
# 048-DQU
print(fake.isbn10())  # ISBN碼
# 1-57202-578-6

# user agent
print(fake.android_platform_token())
# Android 4.1.1
print(fake.ios_platform_token())
# iPhone; CPU iPhone OS 10_3_3 like Mac OS X
print(fake.chrome())
# Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.0 (KHTML, like Gecko) Chrome/38.0.819.0 Safari/535.0
print(fake.user_agent())
# Mozilla/5.0 (Windows 98; ug-CN; rv:1.9.1.20) Gecko/2016-01-13 09:28:45 Firefox/13.0

真的是太多了,這邊不一一列出,
更詳細參數介紹及其他方法請參閱 Faker 官方文件:Standard Providers

還有其他人提供並經過官方認可的,例如"汽車"、"Wi-Fi ESSID"、"Web相關"之類的類別:Community Providers

結語

有沒有覺得 Faker 套件所包含的假資料包羅萬象啊🤣,趕快將此網頁加入"我的最愛(書籤)",搞不好以後就有機會使用到呢~



如有遇到其他問題,或文章內容有誤,歡迎底下留言~😙


參考:
Faker 官方GitHub
Faker 官方文件


You've got to find what you love.
你必須找到你所愛的。

—— 賈伯斯(Steve Jobs)


🔻 如果覺得喜歡,歡迎在下方獎勵我 5 個讚~
分享

Jia
作者
Jia
軟體工程師