Python

How to Use Python Faker to Generate Dummy Data

The dummy data is required to test any application. It is very time-consuming to insert dummy data manually for testing purposes. This task can be done very easily by using the Python Faker package. A large amount of fake data of different types can be inserted into the database very quickly by using this package. The ways of installing and using the Python Faker package have been shown in this tutorial.

Faker Package Installation

The Faker library is not installed in Python by default. It supports Python 3.6+ version only. Run the following command to install the Faker library. You have installed the pip3 package before installing the Faker library.

$ pip3 install Faker

Different types of fake data can be installed by using the Faker library. Some commonly used faker methods are mentioned below.

Faker Method Purpose
name() It is used to generate a fake name.
address() It is used to generate a fake address.
email() It is used to generate fake email
url() It is used to generate a fake url address.
phone_number() It is used to generate a fake phone number.
country() It is used to generate a country name.
text() It is used to generate fake text.
sentence() It is used to generate large text.
date() It is used to generate a dummy date value.
time() It is used to generate a dummy time value.
year() It is used to generate a dummy year value.

Example-1: Generate Different Types of Fake Data

Create a Python file with the following script that will generate the dummy name, email, address, country, and URL address of a person. The faker library has been imported and the faker object has been created to generate the dummy data.

#Import Faker
from faker import Faker

#Create faker object
fake = Faker()

#Print dummy data
print("Name:", fake.name())
print("Email:", fake.email())
print("Address:", fake.address())
print("Country:", fake.country())
print("URL:", fake.url())

Output:

The following output will appear after executing the above script.

Example-2: Write Fake Data into a File

The group of dummy data can be stored in JSON by using a Python script. Create a Python file with the following script that will generate a particular number of dummy records and store the records in a JSON file. The generate_data() function is created in the script to generate a particular number of customer records by using for loop. Here, the customer id of 5 digits will be generated by using the random_number() method. The other values of the customer will be name, address, email, and phone number. All customer data will be stored in a dictionary and stored in the customer.json file by using the JSON module.

#Import Faker
from faker import Faker

#Import JSON
import json
 
#Declare faker onject
fake = Faker()
 
#Define function to generate fake data and store into a JSON file
def generate_data(records):

    #Declare an empty dictionary
    customer ={}

    #Iterate the loop based on the input value and generate fake data
    for n in range(0, records):
        customer[n]={}
        customer[n]['id']= fake.random_number(digits=5)
        customer[n]['name']= fake.name()
        customer[n]['address']= fake.address()
        customer[n]['email']= str(fake.email())
        customer[n]['phone']= str(fake.phone_number())
 
    #Write the data into the JSON file
    with open('customer.json', 'w') as fp:
        json.dump(customer, fp)
 
    print("File has been created.")
 
#Take the number of records from the user
num = int(input("Enter the number of records:"))

#Call the function to generate fake records and store into a json file
generate_data(num)

Output:

The script will take the number of records from the user after execution. The output shows that 5 has been given as the input value and 5 records of customers have been stored in the customer.json file.

Example-3: Use Fake Data Based on Locale

Create a Python file with the following script to generate a dummy phone number based on the locale value initialized at the time of creating the faker object. Here, ‘bn_BD’ is used as the locale value. So, the phone number will be generated based on Bangladesh. The phonenumbers module has been imported in the script to format the phone number based on the country code and this module is not installed by default in Python. So, you have to install the phonenumbers module before executing the script.

#Import phonenumbers module
import phonenumbers

#Import faker module
from faker import Faker

#Create faker object based on locale
fake = Faker(locale="bn_BD")

#Generate fake phone number
number = fake.phone_number()

#Create object to generate phone number based on BD
objPhone = phonenumbers.parse(number, "BD")

#Generate phone number in international format
Phone_number = phonenumbers.format_number(objPhone, phonenumbers.PhoneNumberFormat.INTERNATIONAL)

#Print the phone number
print("Phone number in international format is", Phone_number)

Output:

The following similar output will appear after executing the above script.

Example-4: Read Fake Data from the List

Create a Python file with the following script to generate a dummy sentence three times by re-arranging the values of a list.

#Import faker module
from faker import Faker
 
#Create faker object
fake = Faker()
 
#Define a list
listdata = ["Python", "Java", "Perl", "Bash", "PHP"]
 
#Iterate the loop for three times
for i in range(0, 3):

    #Generate fake data using list data
    fake_data = fake.sentence(ext_word_list = listdata)

    #Print the fake data
    print(fake_data)

Output:

The following similar output will appear after executing the above script.

Example-5: Generate Different Random Numbers

Different types of random numbers can be generated by using the faker library. Create a Python file with the following script that will generate three types of random numbers. The random_int() function will generate a random integer number. The random_number(digit=5) function will generate a random number of 5 digits. The random_int(50, 150) function will generate a random number between 50 to 150.

#Import faker module
from faker import Faker

#Create a faker object
faker = Faker()

#Print different types of fake integer
print("The simple random integer:", faker.random_int())
print("The random integer of particular digits:", faker.random_number(digits=5))
print("The random integer between 50 to 150:", faker.random_int(50, 150))

Output:

The following similar output will appear after executing the above script.

Example-6: Generate the Fake Date and Time

Create a Python file with the following script that will generate different types of date and time-related dummy data. Many methods exist in the faker library to generate dummy date and time. Some of them have been used in this script.

#Import faker module
from faker import Faker

#Create a faker object
faker = Faker()

#Print the date related data
print("Date:", faker.date())
print("The day of the month:", faker.day_of_month())
print("Month Name:", faker.month_name())
print("Year:", faker.year())
print("Weekday name:", faker.day_of_week())

#Print the time related data
print("Time:", faker.time())
print("Time zone:",faker.timezone())
print("AM/PM:", faker.am_pm())

Output:

The following similar output will appear after executing the above script.

Example-7: Generate Fake Profile Data Using Pandas

Sometimes it requires working with a large amount of data set for testing purposes. This task can be done very easily by using faker and pandas modules. Create a Python file with the following script to generate the profile data of 10 persons and store the data in pandas DataFrame.

#Import faker module
from faker import Faker

#Import pandas
import pandas as pd

#Create faker object
faker = Faker()

#Generate profile data
profileData = [faker.profile() for i in range(10)]

#Store profile data in the dataframe
dataframe = pd.DataFrame(profileData)

#Print the profile data
print("The output of the profile data:\n",dataframe)

Output:

The following similar output will appear after executing the above script.

Conclusion

Different uses of the faker module of Python have been described in this tutorial by using multiple examples that will help the Python users to use this module in their script properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.