from faker import Faker
fake = Faker('it_IT')
print("Name,Email,Date of birth")
for _ in range(10):
name = fake.name()
email = fake.email()
dob = fake.date_of_birth()
print(f"{name},{email},{dob}")Faker generates realistic but made-up data: names, addresses, emails,
phone numbers, dates, companies, text and more. Each kind of value is a
method on a Faker object, such as fake.address() or
fake.company(). Developers use it to fill test databases, build demo
datasets and try code on personal data without touching real records. This page is an online Faker compiler: the code runs in your browser,
so you can try it without installing anything.
Run the example first, then paste any snippet below into a new cell to try it.
Faker("it_IT") creates a generator for Italian data, so the names are
Italian and some start with a title such as "Dott." or "Sig.ra". Each
call to fake.name(), fake.email() or fake.date_of_birth() returns a
new random value, and the loop prints ten rows as CSV under a header
line. The email is built from a random user name, so it does not match
the person's name. Without a seed, every run prints different people.
Faker.seed() fixes the random sequence, which is what tests need. It
applies to every Faker object. fake.seed_instance(42) seeds one object
only:
from faker import Faker
fake = Faker()
Faker.seed(42)
first = [fake.name() for _ in range(3)]
Faker.seed(42)
second = [fake.name() for _ in range(3)]
print(first)
print(first == second)
A locale code gives names, addresses and phone numbers in that country's format. A list of codes mixes them, and indexing picks one:
from faker import Faker
Faker.seed(0)
de = Faker("de_DE")
print(de.address())
print(de.phone_number())
mixed = Faker(["en_US", "ja_JP", "pt_BR"])
for _ in range(3):
print(mixed.name())
print(mixed["ja_JP"].city())
faker.config.AVAILABLE_LOCALES lists every supported code.
Generate one dict per row and hand the list to
pandas. date_between() counts back from today by
default, so fixed start and end dates keep the output the same from day
to day:
import datetime
import pandas as pd
from faker import Faker
fake = Faker()
Faker.seed(1)
rows = [
{
"name": fake.name(),
"email": fake.unique.email(),
"signup": fake.date_between(datetime.date(2024, 1, 1), datetime.date(2024, 12, 31)),
"plan": fake.random_element(["free", "pro", "team"]),
"seats": fake.pyint(1, 50),
}
for _ in range(100)
]
df = pd.DataFrame(rows)
df.to_csv("customers.csv", index=False)
print(df.head())
fake.unique.email() makes sure no two rows share an address, and
df.to_csv() writes the table to customers.csv.
DynamicProvider adds a method that picks from your own list.
bothify() fills a pattern, turning each ? into a letter and each #
into a digit, which suits order numbers and codes. numerify() and
lexify() fill digits only or letters only:
from faker import Faker
from faker.providers import DynamicProvider
fake = Faker()
fake.add_provider(DynamicProvider(
provider_name="department",
elements=["Sales", "Support", "Engineering", "Finance"],
))
Faker.seed(3)
for _ in range(3):
print(fake.department(), fake.bothify("ORD-??-####", letters="ABCDEF"))
fake.email() only produces addresses at example.com, example.org and
example.net, unless you pass your own, as in
fake.email(domain="acme.test"). fake.free_email() uses gmail.com,
yahoo.com and hotmail.com instead.date_of_birth() covers ages 0 to 115 by default, which is why the
example can print a child's birth date. Pass minimum_age=18 for
adults.fake.unique never repeats a value and raises UniquenessException
when it runs out, as fake.unique.random_int(1, 3) does on its fourth
call. fake.unique.clear() starts it over.