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.

Revisions

  1. bycoffe revised this gist Sep 14, 2009. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions csvcut
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,8 @@ if args:
    i = open(args[0])
    else:
    i = sys.stdin
    delimiter = ','
    cols = [1]
    delimiter = ','
    cols = [0, ]
    if opts:
    opts = dict(opts)
    if '-f' in opts:
  2. bycoffe revised this gist Sep 14, 2009. 1 changed file with 13 additions and 10 deletions.
    23 changes: 13 additions & 10 deletions csvcut
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,6 @@
    #!/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.
    Note that fields are zero-based, as opposed to 'cut' where they are 1-based.
    @@ -15,19 +12,25 @@ Usage:
    head -10 foobar.csv | csvcut -f 0,2
    (prints the first and third columns of the first ten lines of foobar.csv)
    csvcut -f 0,2 -d "|" foobar.csv
    (prints the first and third columns of the pipe-delimited foobar.csv)
    """
    import sys, csv, getopt
    opts, args = getopt.getopt(sys.argv[1:], "f:", ["fields="])
    opts, args = getopt.getopt(sys.argv[1:], "f:d:", ["fields=", "delimiter="])
    if args:
    i = open(args[0])
    else:
    i = sys.stdin
    delimiter = ','
    cols = [1]
    if opts:
    cols = opts[0][1].split(",")
    else:
    cols = [0]
    for row in csv.reader(i):
    opts = dict(opts)
    if '-f' in opts:
    cols = opts['-f'].split(",")
    if '-d' in opts:
    delimiter = opts['-d']
    for row in csv.reader(i, delimiter=delimiter):
    for c in cols:
    print row[int(c)],
    print

    print
  3. @JoeGermuska JoeGermuska revised this gist Sep 14, 2009. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions csvcut
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,16 @@ 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
    Note that fields are zero-based, as opposed to 'cut' where they are 1-based.
    Leveraged from/motivated by an example from @bycoffe
    Usage:
    csvcut foobar.csv
    (prints the first column of each row of foobar.csv)
    head -10 foobar.csv | csvcut -f 0,2
    (prints the first and third columns of the first ten lines of foobar.csv)
    """
    import sys, csv, getopt
    opts, args = getopt.getopt(sys.argv[1:], "f:", ["fields="])
    @@ -16,7 +25,7 @@ else:
    if opts:
    cols = opts[0][1].split(",")
    else:
    cols = [1]
    cols = [0]
    for row in csv.reader(i):
    for c in cols:
    print row[int(c)],
  4. @JoeGermuska JoeGermuska created this gist Sep 14, 2009.
    24 changes: 24 additions & 0 deletions csvcut
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #!/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