DEV Community

Arpit
Arpit

Posted on

Fake data generator in Python

Faker is a Python package that allows you to generate your own fake dataset.By using this package you can generate random data such as Name,Address,Email,City,Country etc. This package supports multiple locations and languages to generate data based on locality.

Faker is not a standard python library,so first you need to install it using following command:

pip install Faker
Enter fullscreen mode Exit fullscreen mode

Following example is the simple demonstration of Faker to generate fake Name,Date of Birth, Address,country,email.

from faker import Faker
#Faker instance
fake=Faker()
print('Name:',fake.name())
print('Date of Birth:',fake.date_of_birth())
print('Address:',fake.address())
print('Country:',fake.country())
print('Email:',fake.email())
Enter fullscreen mode Exit fullscreen mode

Output

Name:Stephen Palmer
Date of Birth:1984-02-24
Address:880 Jones Wells Suite 398
West Kimberlyfort,WI 86842
Country:Romania
Email:taylorelaine@gmail.com
Enter fullscreen mode Exit fullscreen mode

Faker generates data by default in English US ('en_US)locale.Consider the following example to generate random names in default locale.

 from faker import Faker
 #Faker instance
 fake=Faker()
 print('First Name:',fake.first_name())
 print('Last Name:',fake.last_name())
 print('Name:',fake.name())
Enter fullscreen mode Exit fullscreen mode

Output

 First Name:Jamie
 Last Name:Stephenson
 Name:Julie Davidson
Enter fullscreen mode Exit fullscreen mode

To change the language we need to pass the specific locale to the Faker class.

 from faker import Faker
 #Faker instance in
 #English-India language
 fake Faker('en_In')
 print('First Name:',fake.first_name())
 print('Last Name :, fake.last_name())
 print('Name:',fake.name())
 print('--------------')
 #Faker instance in
 #Hindi-India language
 fake1 Faker('hi_In')
 print('First Name :, fake1.first_name())
 print('Last Name:',fake1.last_name())
 print('Name:',fake1.name())
Enter fullscreen mode Exit fullscreen mode

Output

 First Name:Dhruv
 Last Name:Tripathi
 Name:Sara Dave
--------------
 First Name:राजीव
 Last Name:दूबे
 Name:अमर आहूजा
Enter fullscreen mode Exit fullscreen mode

List of available locales that are supported by Faker library.

 from faker.config import AVAILABLE_LOCALES
 #get list of available locales
 List=[local for local in AVAILABLE_LOCALES]
 #print list
 print(List)
Enter fullscreen mode Exit fullscreen mode

Output

['ar_AA','ar_EG','ar_JO','ar_PS','ar_SA','bg_BG','bs_BA','cs_CZ','de',
 de_AT','de_CH','de_DE','dk_DK','el CY','el_GR','en','en_AU','en_CA',
'en_GB','en_IE','en_IN','en NZ','en_PH','en_TH','en_US','es','es_CA',
'es_ES','es MX','et EE','fa IR','fi_FI','fil PH','fr_CA','fr_CH','fr_F
R','fr_QC','he_IL','hi_IN','hr_HR','hu_HU','hy_AM','id_ID','it_CH','₁
t_IT','ja_JP','ka_GE','ko_KR','la','1b_LU','lt_LT','1v_LV','mt_MT',
e_NP','n1_BE','nl_NL','no_NO','or_IN","pl_PL','pt_BR','pt_PT','ro_RO',
'ru_RU','sk_SK','sl_SI','sv_SE','ca_IN','th','th_TH','el_PH','tr_TR¹,
'tw_GH','uk_UA','zh_CN','zh_TW']
Enter fullscreen mode Exit fullscreen mode

There are lot of methods in Faker,you can get the list of all available attributes and methods using dir()function.

 from faker import Faker
 #Faker instance
 fake=Faker()
 #print list
 print(dir(fake))
Enter fullscreen mode Exit fullscreen mode

Output

 ['__class_','_delattr_',
 '_dir_','__doc____',
                      '_dict_',
                         'address','am_pm',...
 'android platform_token','ascii_company_email',
        'word','words','year','zip',
 'zipcode','zipcode_in_state','zipcode_plus4']
Enter fullscreen mode Exit fullscreen mode

Follow me for more :) Happy Coding

Top comments (2)

Collapse
 
vulcanwm profile image
Medea

This is really cool! I never knew this existed

Collapse
 
soniarpit profile image
Arpit

Thanks :)