Skip to content

Instantly share code, notes, and snippets.

@bbelderbos
Created September 3, 2021 10:09
Show Gist options
  • Save bbelderbos/63326c81cfd439619ac30cfd3c351747 to your computer and use it in GitHub Desktop.
Save bbelderbos/63326c81cfd439619ac30cfd3c351747 to your computer and use it in GitHub Desktop.
import csv
from itertools import cycle
from faker import Faker
fake = Faker()
teachers = cycle([fake.user_name(), fake.user_name()])
classes = cycle(["Math", "English", "Physics"])
def create_fake_student_data(n=10):
rows = []
for _ in range(n):
rows.append({'email': fake.email(domain="pybit.es"),
'class': next(classes),
'teacher': next(teachers)})
return rows
def create_students_csv(rows, filename="students.csv"):
with open(filename, 'w', newline='') as csvfile:
fieldnames = ['email', 'class', "teacher"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
if __name__ == "__main__":
rows = create_fake_student_data(100)
create_students_csv(rows, "students2.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment