Last active
October 14, 2023 02:37
-
-
Save navicore/31089ea0b686447977752a4536bb9253 to your computer and use it in GitHub Desktop.
python to gen test csv files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import random | |
from faker import Faker | |
fake = Faker() | |
# Define a list of more inclusive gender options | |
gender_options = ['Male', 'Female', 'Non-Binary', 'Trans Male', 'Trans Female', 'Genderqueer', 'Genderfluid', 'Agender', 'Other', 'Prefer Not to Say', 'Decline to State'] | |
# Create a list to store the data | |
data = [] | |
# Generate 200 lines of fictitious data | |
for _ in range(200): | |
name = fake.name() | |
street = fake.street_address() | |
city = fake.city() | |
state = fake.state_abbr() | |
zip_code = fake.zipcode() | |
# Generate the occupation with potential commas and sanitize it | |
occupation = fake.job().replace(',', ' ') | |
sex = random.choice(gender_options) # Randomly select from gender_options | |
age = random.randint(18, 65) | |
data.append([name, street, city, state, zip_code, occupation, sex, age]) | |
# Define the CSV file name | |
csv_file = "test_data.csv" | |
# Write the data to a CSV file | |
with open(csv_file, mode='w', newline='') as file: | |
writer = csv.writer(file) | |
# Write the header row | |
writer.writerow(['Name', 'Street Address', 'City', 'State', 'ZIP', 'Occupation', 'Gender', 'Age']) | |
# Write the data | |
writer.writerows(data) | |
print(f"CSV file '{csv_file}' with 200 lines of fictitious data has been generated.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment