How to generate fake data using the Faker module.

How to generate fake data

I recently came across a useful package on PyPI for generating fake data called Faker. Faker can generate all sorts of fake data such as names, placeholder lorem ipsum text, addresses and credit card numbers to name a few things.It can be used to populate a database, anonymise data pulled from records and to produce data to use when testing applications.

Faker is available on PyPI and can be installed by running:
pip install Faker

Here is a code sample that shows how to use Faker:

>>> from faker import Faker
>>> fake = Faker()


>>> fake.name()
u'Adam Marsh'

>>> fake.company()
u'Lewis LLC'

>>> fake.safe_email()
u'judithrichards@example.net'

The data produced by faker can be customised to suite a particular locale. For instance, to produce Italian names and addresses, you pass in the ‘it_IT’ locale as an argument to the Faker() constructor:

>>> from faker import Faker
>>> fake = Faker('it_IT')
>>> for _ in range(10):
...     print(fake.name())


Sig. Alessio Martini
Lorenzo Mazza
Sig. Orfeo Damico
Avide Marini
Demian Ferrara
Brigitta Vitale
Dott. Oretta Rossi
Dott. Kai Parisi
Mirko Donati
Noel Coppola

Faker is a great package that I have found useful and I encourage you to check it out.

1 comment on “How to generate fake data using the Faker module.Add yours →

Leave a Reply