【Python】Python 〜 Faker 〜

◾️はじめに

https://dk521123.hatenablog.com/entry/2025/12/19/233012

で「Faker」っていうのを知ったので調べてみた
(めちゃめちゃ簡単に使える!!)

目次

【1】Faker
【2】インストール
【3】サンプル
 例1:Hello world
 例2:ダミーCSVデータ
【4】Tips
 1)日本語の場合
 2)サポートしているデータ種類

【1】Faker

* 名前、住所、IPアドレスなどの「それっぽい」データを
 生成できるPythonライブラリ

https://faker.readthedocs.io/en/master/

【2】インストール

pip install Faker

【3】サンプル

例1:Hello world

from faker import Faker

fake = Faker()
print("名前:", fake.name())
print("住所:", fake.address())
print("電話番号:", fake.phone_number())
print("メールアドレス:", fake.email())
print("会社名:", fake.company())
print("職業:", fake.job())
print("日付:", fake.date())
print("文章:", fake.text())
print("IPアドレス:", fake.ipv4())
print("クレジットカード番号:", fake.credit_card_number())
print("ユーザーエージェント:", fake.user_agent())
print("ランダムな数値:", fake.random_number(digits=5))
print("ランダムな文字列:", fake.pystr(min_chars=5, max_chars=10))
print("ランダムな選択肢:", fake.random_element(elements=('A', 'B', 'C', 'D')))
print("ランダムな日付:", fake.date_between(start_date='-30days', end_date='today'))
print("ランダムな時間:", fake.time())
print("ランダムな日時:", fake.date_time_between(start_date='-30days', end_date='now'))
print("ランダムなURL:", fake.url())
print("ランダムなUUID:", fake.uuid4())
print("ランダムなパスワード:", fake.password(length=10, special_chars=True))

出力結果

名前: Regina Aguirre
住所: 3671 Lacey Mews
East Jennifer, WY 76820
電話番号: 487-658-6886
メールアドレス: yesenia94@example.com
会社名: Hess Group
職業: Soil scientist
日付: 2013-07-11
文章: Current street financial question past brother next. Top our remain indeed since.
Name into plan somebody wonder. South husband seat someone sure success suffer even.
IPアドレス: 126.247.28.137
クレジットカード番号: 346877590695106
ユーザーエージェント: Mozilla/5.0 (compatible; MSIE 5.0; Windows NT 11.0; Trident/3.1)
ランダムな数値: 65692
ランダムな文字列: pTTcn
ランダムな選択肢: A
ランダムな日付: 2025-12-17
ランダムな時間: 23:30:22
ランダムな日時: 2025-12-17 16:50:32.905715
ランダムなURL: http://brown-davis.biz/
ランダムなUUID: 90b875a5-66cd-47d0-99e7-a5d58159e70b
ランダムなパスワード: _50JUKpx_0

例2:ダミーCSVデータ

from faker import Faker
import csv

fake = Faker('en_US')

DATA_SIZE = 100000

with open('sample.csv', 'w', newline="") as csv_file:
  writer = csv.writer(csv_file, delimiter=",")
  writer.writerow([
    "id",
    "name",
    "gender",
    "birth_day",
    "address",
    "phone_number",
    "email",
  ])

  for _ in range(DATA_SIZE):
    writer.writerow([
      fake.unique.random_int(min=1, max=DATA_SIZE*2),
      fake.name(), 
      fake.random_element(elements=['m', 'f']),
      fake.date_of_birth(minimum_age=18, maximum_age=80),
      fake.address().replace("\n", " "),
      fake.phone_number(),
      fake.email()
    ])

print("Done!")

出力結果(sample.csv

id,name,gender,birth_day,address,phone_number,email
191134,Anna Mccann,m,1983-01-08,"71219 Kenneth Walks Apt. 986 Lambton, ND 45059",445.983.0048x1784,haley61@example.net
163461,William Harris,m,1963-09-19,"4192 Graham Well Alexandermouth, AL 26832",949-754-8737x003,muellerjoshua@example.com
160894,Amanda Mccormick,m,1950-02-01,"94593 Carroll Ports New Sherri, PR 69342",534.850.6645x106,alanalvarado@example.org
193521,Cynthia Mann,f,1998-03-03,"77273 Love Rapid Lisaville, GA 58934",924-557-0358x351,anguyen@example.net
133179,Samantha Hunter,f,1953-07-10,"6452 Harris Gateway Suite 473 West Kaylashire, ID 70065",001-244-406-8956,handerson@example.com
129865,Dwayne Adams,m,1986-03-11,"PSC 6588, Box 2061 APO AP 37292",238-916-7881,gonzalezdarin@example.org
161164,Linda Davidson,m,1971-07-28,"7026 Michael Mount Suite 002 Lake Amy, OH 72624",001-946-602-3956x883,louis43@example.org
・・・

【4】Tips

1)日本語の場合

# これだけ!
fake = Faker('ja_JP')

# 複数も可能
# fake = Faker(['it_IT', 'en_US', 'ja_JP'])

https://faker.readthedocs.io/en/master/locales.html

2)サポートしているデータ種類

* 以下を参考に見ておくといいかも

https://faker.readthedocs.io/en/master/#contents

参考文献

https://dev.classmethod.jp/articles/python-faker-for-sampledata/
https://qiita.com/yunosuken/items/5835b6ba26981c56eeda
https://qiita.com/automation2025/items/f5e277fa8cef57f898c2

関連記事

AWS上に合成データ生成システムを構築する 〜 構想編 〜
https://dk521123.hatenablog.com/entry/2025/12/19/233012
Python SDV 〜 入門編 〜
https://dk521123.hatenablog.com/entry/2025/12/21/000330
Python SDV 〜 HMASynthesizer 〜
https://dk521123.hatenablog.com/entry/2025/12/26/001812
Python ~ 基本編 / CSV
https://dk521123.hatenablog.com/entry/2019/11/07/214108