Skip to content

Instantly share code, notes, and snippets.

@mgronhol
Forked from kauppim/csv_org.py
Created September 6, 2012 11:21
Show Gist options
  • Save mgronhol/3655037 to your computer and use it in GitHub Desktop.
Save mgronhol/3655037 to your computer and use it in GitHub Desktop.
Pythonized version of kauppim's code
#!/usr/bin/env python
import sys, csv
# Checking for cli parametres
if len( sys.argv ) < 4:
print "Usage: %s input.csv column-label output.csv"%( sys.argv[0] )
sys.exit( 1 )
# Trying to open input file
try:
reader = csv.DictReader( open( sys.argv[1], 'rb' ) )
except IOError:
print >>sys.stderr, "Unable to open file %s"% sys.argv[1]
sys.exit( 1 )
# Grab the column key
column_key = sys.argv[2]
# Trying to open output file
try:
writer = csv.writer( open( sys.argv[3], 'wb' ) )
except IOError:
print >> sys.stderr, "Cannot open file %s for writing." % sys.argv[3]
# Pick all rows that contain the column key
rows = [ row for row in reader if column_key in row ]
# If no rows picked
if len( rows ) < 1:
print >>sys.stderr, "No rows / no suitable rows found!"
sys.exit( 1 )
# Sort rows by value in column key column
sorted_rows = sorted( rows, key = lambda x : x[ column_key ] )
# Extract lables (= approximation, just use first row keys )
labels = rows[0].keys()
# Write labels as first line
writer.writerow( labels )
# Go through every line in sorted rows
for row in sorted_rows:
# Put values in columns in the same order as labels
line = [ row[ label ] for label in labels ]
writer.writerow( line )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment