-
-
Save bycoffe/187278 to your computer and use it in GitHub Desktop.
Revisions
-
bycoffe revised this gist
Sep 8, 2010 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -50,6 +50,7 @@ delimiter = ',' output_delimiter = ' ' cols = [0, ] show_headers = False quotechar = None if opts: opts = dict(opts) @@ -64,8 +65,6 @@ if opts: output_delimiter = opts['-o'] if '-q' in opts: quotechar = opts['-q'] for row in csv.reader(i, delimiter=delimiter, quotechar=quotechar): if show_headers: -
bycoffe revised this gist
Sep 1, 2010 . 1 changed file with 17 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -16,6 +16,10 @@ Usage: csvcut -f 0,2 -d "|" foobar.csv (prints the first and third columns of the pipe-delimited foobar.csv) csvcut -f 0,2 -t foobar.csv (prints the first and third columns of the tab-delimited foobar.csv if present, the -d option will be ignored.) csvcut -h foobar.csv (prints the values of the first line of foobar.csv, preceded by the field index which would be used to display that column. If present, the -f option will be ignored.) @@ -31,9 +35,12 @@ Usage: csvcut -f : -o "|" foobar.csv (prints all the columns of the comma-delimited foobar.csv; output will be pipe-delimited.) csvcut -f 0,1 -d "," -q "|" foobar.csv (prints the first two columns of the comma-delimited, pipe-quoted foorbar.csv.) """ import sys, csv, getopt opts, args = getopt.getopt(sys.argv[1:], "f:d:o:q:ht", []) if args: i = open(args[0]) else: @@ -49,17 +56,23 @@ if opts: show_headers = '-h' in opts if '-f' in opts: cols = opts['-f'].split(",") if '-t' in opts: delimiter = "\t" elif '-d' in opts: delimiter = opts['-d'] if '-o' in opts: output_delimiter = opts['-o'] if '-q' in opts: quotechar = opts['-q'] else: quotechar = None for row in csv.reader(i, delimiter=delimiter, quotechar=quotechar): if show_headers: for i,c in enumerate(row): print "%3i: %s" % (i,c) break writer = csv.writer(sys.stdout, delimiter=output_delimiter) if cols == [':']: cols = range(len(row)) writer.writerow([row[int(c)] for c in cols]) -
bycoffe revised this gist
Sep 15, 2009 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -27,6 +27,10 @@ Usage: csvcut -f 0,1,2 -o "|" foobar.csv (prints the first three columns of the comma-delimited foobar.csv; output will be pipe-delimited.) csvcut -f : -o "|" foobar.csv (prints all the columns of the comma-delimited foobar.csv; output will be pipe-delimited.) """ import sys, csv, getopt opts, args = getopt.getopt(sys.argv[1:], "f:d:o:h", []) @@ -56,4 +60,6 @@ for row in csv.reader(i, delimiter=delimiter): print "%3i: %s" % (i,c) break writer = csv.writer(sys.stdout, delimiter=output_delimiter) if cols == [':']: cols = range(len(row)) writer.writerow([row[int(c)] for c in cols]) -
bycoffe revised this gist
Sep 15, 2009 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -34,9 +34,12 @@ if args: i = open(args[0]) else: i = sys.stdin delimiter = ',' output_delimiter = ' ' cols = [0, ] show_headers = False if opts: opts = dict(opts) show_headers = '-h' in opts @@ -46,6 +49,7 @@ if opts: delimiter = opts['-d'] if '-o' in opts: output_delimiter = opts['-o'] for row in csv.reader(i, delimiter=delimiter): if show_headers: for i,c in enumerate(row): -
bycoffe revised this gist
Sep 15, 2009 . 1 changed file with 15 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,8 +4,6 @@ Like cut, but for CSVs. To be used from a shell command line. Note that fields are zero-based, as opposed to 'cut' where they are 1-based. Should use something better than getopt, but this works... Usage: @@ -21,14 +19,23 @@ Usage: csvcut -h foobar.csv (prints the values of the first line of foobar.csv, preceded by the field index which would be used to display that column. If present, the -f option will be ignored.) csvcut -f 0,1,2 -d "|" -o , foobar.csv (prints the first three columns of the pipe-delimited foobar.csv; output will be comma-delimited.) csvcut -f 0,1,2 -o "|" foobar.csv (prints the first three columns of the comma-delimited foobar.csv; output will be pipe-delimited.) """ import sys, csv, getopt opts, args = getopt.getopt(sys.argv[1:], "f:d:o:h", []) if args: i = open(args[0]) else: i = sys.stdin delimiter = ',' output_delimiter = ' ' cols = [0, ] if opts: opts = dict(opts) @@ -37,12 +44,12 @@ if opts: cols = opts['-f'].split(",") if '-d' in opts: delimiter = opts['-d'] if '-o' in opts: output_delimiter = opts['-o'] for row in csv.reader(i, delimiter=delimiter): if show_headers: for i,c in enumerate(row): print "%3i: %s" % (i,c) break writer = csv.writer(sys.stdout, delimiter=output_delimiter) writer.writerow([row[int(c)] for c in cols]) -
JoeGermuska revised this gist
Sep 14, 2009 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -32,7 +32,7 @@ delimiter = ',' cols = [0, ] if opts: opts = dict(opts) show_headers = '-h' in opts if '-f' in opts: cols = opts['-f'].split(",") if '-d' in opts: -
JoeGermuska revised this gist
Sep 14, 2009 . 1 changed file with 14 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,6 +6,8 @@ Note that fields are zero-based, as opposed to 'cut' where they are 1-based. Leveraged from/motivated by an example from @bycoffe Should use something better than getopt, but this works... Usage: csvcut foobar.csv (prints the first column of each row of foobar.csv) @@ -15,9 +17,13 @@ Usage: csvcut -f 0,2 -d "|" foobar.csv (prints the first and third columns of the pipe-delimited foobar.csv) csvcut -h foobar.csv (prints the values of the first line of foobar.csv, preceded by the field index which would be used to display that column. If present, the -f option will be ignored.) """ import sys, csv, getopt opts, args = getopt.getopt(sys.argv[1:], "f:d:h", []) if args: i = open(args[0]) else: @@ -26,11 +32,17 @@ delimiter = ',' cols = [0, ] if opts: opts = dict(opts) show_headers = bool('-h' in opts or '--header' in opts) if '-f' in opts: cols = opts['-f'].split(",") if '-d' in opts: delimiter = opts['-d'] for row in csv.reader(i, delimiter=delimiter): if show_headers: for i,c in enumerate(row): print "%3i: %s" % (i,c) break for c in cols: print row[int(c)], print -
bycoffe revised this gist
Sep 14, 2009 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 = [0, ] if opts: opts = dict(opts) if '-f' in opts: -
bycoffe revised this gist
Sep 14, 2009 . 1 changed file with 13 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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. 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:d:", ["fields=", "delimiter="]) if args: i = open(args[0]) else: i = sys.stdin delimiter = ',' cols = [1] if opts: 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 -
JoeGermuska revised this gist
Sep 14, 2009 . 1 changed file with 11 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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. 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 = [0] for row in csv.reader(i): for c in cols: print row[int(c)], -
JoeGermuska created this gist
Sep 14, 2009 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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