Skip to content

Instantly share code, notes, and snippets.

@apurvc
Created September 23, 2024 08:43
Show Gist options
  • Save apurvc/6ff91df5deddf7a756c2fb223cc08835 to your computer and use it in GitHub Desktop.
Save apurvc/6ff91df5deddf7a756c2fb223cc08835 to your computer and use it in GitHub Desktop.
from faker import Faker
import random
import csv
fake = Faker()
# Number of records to generate
num_records = 1000
with open('complex_sample_data.csv', mode='w', newline='') as file:
writer = csv.writer(file)
# Write the header
writer.writerow(['user_id', 'name', 'phone_numbers', 'settings'])
for _ in range(num_records):
# Generating random user_id
user_id = random.randint(1, 10000)
# Generating a STRUCT for name
name = {
'first_name': fake.first_name(),
'last_name': fake.last_name()
}
# Generating an ARRAY of phone numbers
phone_numbers = [fake.phone_number() for _ in range(random.randint(1, 3))]
# Generating a MAP for settings
settings = {fake.word(): fake.word() for _ in range(random.randint(1, 4))}
# Convert the complex structures to JSON-like strings for CSV format
name_str = f'{{first_name: {name["first_name"]}, last_name: {name["last_name"]}}}'
phone_numbers_str = f'[{", ".join([f"\'{phone}\'" for phone in phone_numbers])}]'
settings_str = f'{{{", ".join([f"\'{k}\': \'{v}\'" for k, v in settings.items()])}}}'
# Write the row
writer.writerow([user_id, name_str, phone_numbers_str, settings_str])
print("Complex sample data generated and saved to 'complex_sample_data.csv'.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment