Skip to content

Instantly share code, notes, and snippets.

@rwilcox
Created June 28, 2010 04:34
Show Gist options
  • Save rwilcox/455453 to your computer and use it in GitHub Desktop.
Save rwilcox/455453 to your computer and use it in GitHub Desktop.
Create CSV files with random data in them
#!/usr/bin/env python
# encoding: utf-8
"""
create_cvs_files.py
Create a number of fake CVS files. These files have an arbitrary length (from
5 lines to 600), and each have 6 columns.
Requires python-faker (http://github.com/threadsafelabs/python-faker)
Created by Ryan Wilcox on 2010-06-27.
Please consider this code as part of the public domain
"""
import sys
import os
import faker
import csv
import random
def randomEnumerator():
howManyRows = random.randrange(0, 600)
for currentRowNumber in range(0, howManyRows):
yield currentRowNumber
def main(howManyFiles, pathArray):
filesGoIn = "".join(pathArray)
for currentNumber in range( 0, int(howManyFiles) ):
f = open( filesGoIn + str(currentNumber) + ".csv", 'w+b' )
writer = csv.DictWriter(f, fieldnames=
["name", "phone", "address", "city", "state", "zip"])
for current in randomEnumerator():
writer.writerow( dict( name=faker.name.name(),
address=faker.address.street_address(),
phone=faker.phone_number.phone_number(),
city=faker.address.city(),
state=faker.address.us_state_abbr(),
zip=faker.address.zip_code() ) )
f.close()
if __name__ == '__main__':
main( sys.argv[1], sys.argv[2:] )
@brandontan
Copy link

hi,

I am getting the following error:

Traceback (most recent call last):
File "create_fake_csv_files.py", line 46, in
main( sys.argv[1], sys.argv[2:] )
IndexError: list index out of range

I am using python 3.5. Thanks !

@arlejeun
Copy link

Same with Python 2.7.12

@susmita2808
Copy link

with open('D:\ML_docu\PROJECT\EASY CHART.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if int(row['Calories'])<r :
if(p[i]==500) :
next_index = list(islice(csv.reader(csvfile), None, 2))
last_rows = list(csv.reader(csvfile, delimiter=';'))
for row in next_index:
print(row[0])

In this case how will I get random value?? I am getting same value every time, but I want random value? i am using python 3.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment