Created
March 2, 2011 01:49
-
-
Save hjst/850309 to your computer and use it in GitHub Desktop.
dirty csv test data gen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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