post-thumb

Additional Library In Python

In this Tutorial we will learn about the some important library in python and their different uses in real time projects.

1.Python Facker Library

Facker is a Python package that generates fake data for you.

Faker has the ability to print/get a lot of different fake data, for instance, it can print fake name, address, email, text, etc.

pip install Faker
from faker import Faker
#'hi_IN' changed the language
# fake = Faker('hi_IN')   
fake = Faker()
# fake.seed(1)   #if you need same data again and again you can used seed() method 
print (fake.email())
print(fake.country())
print(fake.name())
print(fake.text())
print(fake.latitude(), fake.longitude())
print(fake.url())
print(fake.profile())
print(fake.sentence()) # to print random sentence
"""
if you want the sentence only from particular words then
"""
# List has words that we want in our sentence 
word_list = ["Nepal", "Engineer",
             "Salary", "Messi", "Cricket",
             "family"]
# lets create 5 sentence from the above words 
for i in range(0, 5): 
    # You need to use ext_word_list = listnameyoucreated 
    print(fake.sentence(ext_word_list = word_list)) 

Lets insert data using facker Library


from faker import Faker 
  
# To create a json file
import json        
  
# For student id 
from random import randint     
  
fake = Faker() 
  
def input_data(x): 
  
    # dictionary 
    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) 
  
    # dictionary dumped as json in a json file 
    with open('students.json', 'w') as fp: 
        json.dump(student_data, fp) 
      
  
def main(): 
  
    # Enter number of students 
    # For the above task make this 100 
    number_of_students = 10 
    input_data(number_of_students) 
main() 
# The folder or location where this python code 
# is save there a students.json will be created 
# having 10 students data.

https://faker.readthedocs.io/en/master/index.html