Created
February 4, 2013 01:37
-
-
Save RWJMurphy/4704580 to your computer and use it in GitHub Desktop.
A cut(1)-style tool for working with CSV files
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/env python | |
from __future__ import print_function | |
import argparse | |
import csv | |
import os | |
import sys | |
def fields(string): | |
parsed_fields = [] | |
field_strings = string.split(',') | |
for field in field_strings: | |
if '-' in field: | |
(start, end) = map(int, field.split('-')) | |
parsed_fields.extend(range(start, end + 1)) | |
else: | |
parsed_fields.append(int(field)) | |
return parsed_fields | |
def main(args): | |
exit_code = os.EX_OK | |
infiles = args.file | |
selected_fields = args.fields | |
delimiter = args.output_delimiter | |
if delimiter is None: | |
output = csv.writer(sys.stdout) | |
else: | |
output = sys.stdout | |
for infile in infiles: | |
if infile == '-': | |
infile = sys.stdin | |
else: | |
infile = open(infile, 'rb') | |
reader = csv.reader(infile) | |
for row in reader: | |
out = [] | |
if selected_fields is None: | |
out = row | |
else: | |
for field in selected_fields: | |
try: | |
out.append(row[field - 1]) | |
except IndexError: | |
pass | |
if delimiter is None: | |
output.writerow(out) | |
else: | |
print(delimiter.join(out), file=output) | |
sys.stdout.flush() | |
return exit_code | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--fields', '-f', type=fields) | |
parser.add_argument('--output-delimiter', '-o', type=str) | |
parser.add_argument('file', nargs="*", default=['-']) | |
args = parser.parse_args() | |
try: | |
exit_code = main(args) | |
except IOError as ioe: | |
if ioe.errno == 32: # broken pipe | |
pass | |
else: | |
raise | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment