Skip to content

Instantly share code, notes, and snippets.

@AlanHohn
Created January 19, 2017 13:59
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AlanHohn/293c98f9dadfc67443b8078d843d4401 to your computer and use it in GitHub Desktop.
Save AlanHohn/293c98f9dadfc67443b8078d843d4401 to your computer and use it in GitHub Desktop.
Generate a random CSV in Python
#!/usr/bin/python
import csv
import random
records=9000000
print("Making %d records\n" % records)
fieldnames=['id','name','age','city']
writer = csv.DictWriter(open("people.csv", "w"), fieldnames=fieldnames)
names=['Deepak', 'Sangeeta', 'Geetika', 'Anubhav', 'Sahil', 'Akshay']
cities=['Delhi', 'Kolkata', 'Chennai', 'Mumbai']
writer.writerow(dict(zip(fieldnames, fieldnames)))
for i in range(0, records):
writer.writerow(dict([
('id', i),
('name', random.choice(names)),
('age', str(random.randint(24,26))),
('city', random.choice(cities))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment