Skip to content

Instantly share code, notes, and snippets.

@bycoffe
Forked from JoeGermuska/csvcut
Created September 14, 2009 18:19
Show Gist options
  • Select an option

  • Save bycoffe/186819 to your computer and use it in GitHub Desktop.

Select an option

Save bycoffe/186819 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Like cut, but for CSVs. To be used from a shell command line.
Change row[1] to the row index to be printed. row[1] will print the second
item in the row.
Leveraged from an example from @bycoffe
"""
import sys, csv, getopt
opts, args = getopt.getopt(sys.argv[1:], "f:", ["fields="])
if args:
i = open(args[0])
else:
i = sys.stdin
if opts:
cols = opts[0][1].split(",")
else:
cols = [1]
for row in csv.reader(i):
for c in cols:
print row[int(c)],
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment