Skip to content

Instantly share code, notes, and snippets.

@hjst
Created March 2, 2011 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjst/850309 to your computer and use it in GitHub Desktop.
Save hjst/850309 to your computer and use it in GitHub Desktop.
dirty csv test data gen
#! /usr/bin/env python
from optparse import OptionParser
import sys, csv, os.path, random
def main():
usage = "usage: %prog [options] arg"
parser = OptionParser(usage)
parser.add_option("-t", "--template", dest="filename",
help="use FILENAME as CSV template")
parser.add_option("-d", "--date", dest="date",
help="use DATE as date field")
# any more preamble goes here
(options, args) = parser.parse_args()
if os.path.isfile(options.filename):
reader = csv.reader(open(options.filename, "rb"))
writer = csv.writer(sys.stdout)
# first write the headers out
writer.writerow( reader.next() )
# now print the rest of the file:
for line in reader:
line[0] = options.date # set the date
# line[1] is the unchanged page url
line[2] = random.randint(200,5000)
line[3] = random.randint(150,4000)
line[4] = random.uniform(30,1000)
line[5] = random.random()
line[6] = random.random()
writer.writerow(line)
else:
print "Could not read file %s" % options.filename
sys.exit(2)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment