Python Faker Library

Python Methods and Functions

Installation: Help Link
Open Anaconda Command Prompt to install:

 conda install - c conda-forge faker 

Import Package

 from faker import Faker 

Faker has the ability to print / receive many different fake data, for example, it can print out a fake name, address, email, text, etc.

Important most commonly used fake commands

 fake.name () fake.address () fake .email () fake.text () fake.country () 

from faker import Faker

fake = Faker ()

print (fake.email ())

print (fake.country ())

print (fake.name ())

print (fake.text ())

print (fake.latitude (), fake.longitude ())

print (fake.url ())

 OUTPUT: (Different every time) [email protected] Belgium Shane Hunter Commodi vel libero placeat quibusdam odio odio consequatur. Ducimus libero quae optio non quidem. Facilis quas impedit quo. 26.5687745 -124.802165 http://www.turner.com/ 

Appendix 1: Create a json from 100 students named student.json that contains name, address, location coordinates and student number.

from faker import Faker

import json  # To generate a JSON file

from random import randint  # For student

fake = Faker ()

def input_data (x):

  

# Glossary

  student_data = {}

for i in range ( 0 , x):

student_data [i] = {}

  student_data [i] [ 'id' ] = randint ( 1 , 100 )

  student_data [i] [ ' name' ] = fake.name ()

student_data [i] [ 'address' ] = fake.address ()

student_data [i] [ 'latitude' ] = str (fake.latitude ())

  student_data [i] [ ' longitude' ] = str (fake.longitude ())

  print (student_data)

 

# the dictionary is dumped as json to a json file

with open ( 'students.json' , ' w' ) as fp:

  json.dump (student_data, fp)

 

 

def main ():

 

# Enter the number of students

number_of_students = 10   # For the above task, do this is 100

input_data (number_of_students)

main ()
# The folder or location where this Python code is located
# save the student.json file there
# With 10 student data.

 OUTPUT {0: {'id': 20,' name': 'Benjamin Washington',' address': 'USCGC Garrison FPO AP 48025-9793', 'latitude':' -68.975800', 'longitude':' 153.009590'}, 1: {'id': 2,' name': 'Christopher Howell',' address': '7778 Sarah Center Apt ... 663 Lawrenceport, WY 78084', 'latitude':' -21.8141675', 'longitude':' -122.830387'}, 2: {'id': 67,' name': 'Fernando Fuentes',' address': '7756 Bradford Plain Suite 997 East Chelseaburgh, KY 75776', 'latitude':' -82.791227', 'longitude':' -42.964122'}, 3: {'id': 86,' name': 'Patrick Torres',' address ':' Unit 5217 Box 7477 DPO AE 82354-0160', 'latitude':' 34.949096', 'longitude':' 121.715387'}, 4: {'id': 11,' name': 'James Hines',' address': '4567 Donald Grove Williamhaven, MO 85891',' latitude': '86.7208035',' longitude': '-48.103935'}, 5: {' id': 33, 'name':' James Miller', ' address': 'PSC 2613, Box 7165 APO AP 29256-6576',' latitude': '-35.4630595',' longitude': '-50.415667'}, 6: {' id': 76, 'name':' Randall Fuller', 'address':' 7731 Garcia Pike New Eric, KS 20545', 'latitude':' 12.198124', 'longitude':' 126.720134'}, 7: {'id': 49,' name': 'Ivan Franco', 'address':' 801 Chambers Light West Daniel, IA 17114-4374', 'latitude':' -58.2576055', 'longitude':' 171.773233'}, 8: {'id': 75,' nam e': 'Amy Smith',' address': '995 Luna Stream Apt. 297 Thompsonchester, NY 82115', 'latitude':' 80.4262245', 'longitude':' 115.142004'}, 9: {'id': 38,' name': 'Danielle Thomas',' address': '7309 Chris Ferry Suite 674 Colebury, MA 39673-2967', 'latitude':' -73.340443', 'longitude':' -176.964241'}} 

Appendix 2: Type 10 fake names and countries in Hindi .

from faker import Faker

fake = Faker ( 'hi_IN' # & # 39; hi_IN & # 39; changed language

for i in range ( 0 , 10 ):

  print ( 'Name- & gt;' , fake.name (), ' Country- & gt; ' , fake.country ())

Appendix 3: Create a fake profile

 OUTPUT {'job':' Town planner', 'company':' Martinez-Clark', 'ssn':' 559-93-0521', 'residence':' 46820 Johnny Circles Stokesside, IL 87065-2470', 'current_location': (Decimal (' 83.5271055'), Decimal ('43.705455')),' blood_group': 'A +', 'website': [' https:// www.taylor.com / '],' username': 'hsmith',' name': 'Christopher Davis',' sex': 'M',' address': '335 Mcdaniel Fork Suite 589 Teresabury, AZ 85283', 'mail':' kenneth48 @ yahoo.com', 'birthdate':' 1981-03-29'} 

Appendix 4: Populating the Generator, getting certain bogus data again.
Padding makes use of the same fake data result that was generated the first time with this seed number. 
example

import faker from Faker

 

fake = Faker ()

print (fake.profile ())

from faker import Faker

fake = Faker ()

 

fake.seed ( 1 )

print (fake.name ())

print (fake.address ())

print (fake.email ())

 OUTPUT Ryan Gallagher 7631 Johnson Village Suite 690 Adamsbury, NC 50008 bparks @ johnson .info 

NOTE. Even if I run the program over and over again, I get the same result. As soon as I remove this fake.seed (1) line, we see randomness in the data generation.

Appendix 5: Print the data from the list you want.

import faker from Faker

fake = Faker ()

# Print random sentences

print (fake.sentence ())

 
# The list contains the words we want in our sentence

word_list = [ "GFG" , "G eeksforgeeks " , " shaurya " , "says" , "Gfg" , "GEEKS" ]

  
# Let's print 5 sentences that contain words from our word_list

for i in range ( 0 , 5 ):

 

# You need to use ext_word_list = listnameyoucreated

  pr int (fake.sentence (ext_word_list = word_list))

 OUTPUT # This is the random sentence that is generated using # fake.sentence () Error architecto inventore aut. # These are the 5 sentence that contains words from # word_list we provided Shaurya shaurya GEEKS Geeksforgeeks. Gfg shaurya Geeksforgeeks GFG Gfg GFG. Geeksforgeeks Gfg says Geeksforgeeks GEEKS Gfg Gfg GFG. Geeksforgeeks shaurya GFG Geeksforgeeks Gfg GEEKS. Gfg Geeksforgeeks says GFG GEEKS says. 

Summary of what we learned from Faker
1. Fake data generations like name, address, email, text, sentence, etc.
2. Creation of fake data JSON file. 
3. Various language fake data is printed. 
4. Creation of a profile.
5. Sowing, i.e. printing certain fake data
6. Create a sentence that contains the words we have provided.