Skip to content

Instantly share code, notes, and snippets.

@alismx
Last active April 7, 2023 20:29
Show Gist options
  • Save alismx/ea13a03b628e2d682334c0adf38400c5 to your computer and use it in GitHub Desktop.
Save alismx/ea13a03b628e2d682334c0adf38400c5 to your computer and use it in GitHub Desktop.
Python script to generate fake data
# `pip install faker`
# OR
# specify fake in your requirements.txt file
# then run `pip install -r requirements.txt`
#!/usr/bin/python3
from faker import Faker
import csv
fake = Faker()
num_rows = 10 # change this value to specify the number of rows required
filename = "fake_data.csv"
# Specify the header row for the CSV file
header = ['first_name', 'middle_name', 'last_name', 'ssn', 'gender', 'street_address', 'apt_number', 'city', 'state', 'phone_number']
# Generate fake data for all rows
rows = []
for i in range(num_rows):
row = {'first_name': fake.first_name(), 'middle_name': fake.first_name(), 'last_name': fake.last_name(),
'ssn': fake.ssn(), 'gender': fake.random_element(elements=('Male', 'Female')),
'street_address': fake.street_address(), 'apt_number': fake.building_number(),
'city': fake.city(), 'state': fake.state(), 'phone_number': fake.phone_number()}
rows.append(row)
# Write to CSV file using DictWriter
with open(filename, mode='w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=header)
writer.writeheader()
writer.writerows(rows)
print(f"Generated {num_rows} rows of fake data and saved to {filename}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment