Created
March 25, 2011 17:01
-
-
Save hownowstephen/887186 to your computer and use it in GitHub Desktop.
Python command line CSV browser
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/python | |
""" | |
csvbrowse.py | |
Acts as a command line csv browser, allows you to view only a select set of columns | |
and open from a particular line | |
Usage: | |
csvbrowse.py -f <csvfile> -H | |
displays a listing of the headers of the file | |
csvbrowse.py -f <csvfile> -c 1,2 | |
starts by displaying the values of columns 1 and 2 at line 1 (skips header) | |
csvbrowse.py -f <csvfile> -c 1,2 -i | |
same as prev, except contents are displayed comma separated and inline | |
csvbrowse.py -f <csvfile> -c 1,2 -s 10 | |
same as example 2, but starts at the 10th line of the file | |
""" | |
import csv, sys | |
from optparse import OptionParser | |
parser = OptionParser() | |
parser.add_option("-c", "--columns", | |
action="store", dest="columns", | |
help="comma separated set of columns to display") | |
parser.add_option("-i", "--inline", action="store_true", dest="inline", | |
default=False, help="display each on a separate line") | |
parser.add_option("-f", "--file", action="store", dest="file", | |
help="the file to parse") | |
parser.add_option("-n", "--noheaders", action="store_false", | |
default=True, help="skip the first line of the file") | |
parser.add_option("-H", "--headerlisting", action="store_true", | |
default=False, dest="headerlisting", help="display a list of headers") | |
parser.add_option("-s", "--start", action="store", dest="startline", | |
default="0", help="set what line to start printing from" ) | |
(options, args) = parser.parse_args() | |
if options.columns is not None: | |
cols = options.columns.split(',') | |
else: | |
cols = [] | |
# Open the CSV file | |
csvobj = csv.reader(open(options.file,'rb'),delimiter=',',quotechar='"') | |
# Headerlisting option shows the set of available column headers | |
if options.headerlisting: | |
print "Headers for %s" % options.file | |
index = 0 | |
for row in csvobj: | |
for header in row: | |
print "[%i] %s" % (index,header) | |
index = index + 1 | |
break | |
sys.exit() | |
print "\n" | |
count = -1 | |
# loop through the csv rows | |
for row in csvobj: | |
count = count + 1 | |
out = "[%i]" % count | |
# Skips the first line if noheaders is set | |
if options.noheaders and not count: continue | |
# Skips all lines before the supplied startline | |
if count < int(options.startline): continue | |
# Print all the specified columns (either one per line or inline) | |
for col in cols: | |
if not options.inline: | |
print row[int(col)] | |
else: | |
out = "%s, %s" % (out,row[int(col)]) | |
if options.inline: print out | |
input = raw_input("\nLine %i - [enter] or [q]: " % count) | |
print "\n" | |
if input == 'q': break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment