Created
March 19, 2014 16:12
-
-
Save eponvert/9645191 to your computer and use it in GitHub Desktop.
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 characters
#!/usr/bin/env python2.7 | |
# Copyright (c) 2014 Elias Ponvert | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
from argparse import ArgumentParser | |
from csv import reader as csv_reader, writer as csv_writer | |
__author__ = "Elias Ponvert" | |
__copyright__ = "Copyright 2014, Elias Ponvert" | |
__version__ = 'v0.1.0' | |
__license__ = "MIT" | |
if __name__ == '__main__': | |
ap = ArgumentParser(description="Like paste, for CSVs and TSVs and what not") | |
ap.add_argument('--version', action='version', version='%(prog)s ' + __version__) | |
ap.add_argument('-d', '--delimiter', metavar='DELIM', default='\t', | |
help="use DELIM instead of TAB for field delimiter") | |
ap.add_argument('--output-delimiter', metavar='STRING', default=None, | |
help="use STRING as the output delimiter, the default is to use the input delimiter") | |
ap.add_argument('--skip-header', action='store_true', help="skip first row of input") | |
ap.add_argument('files', metavar='FILE', nargs='*', default=None, help="csv/tsv file input") | |
import sys | |
args = ap.parse_args(sys.argv[1:]) | |
input_delim = args.delimiter | |
output_delim = args.output_delimiter if args.output_delimiter else input_delim | |
file_handles = [open(f, 'rU') for f in args.files] | |
csv_readers = [csv_reader(fh_in, delimiter=input_delim) for fh_in in file_handles] | |
if args.skip_header: | |
[reader.next() for reader in csv_readers] | |
csv_out = csv_writer(sys.stdout, delimiter=output_delim) | |
try: | |
while True: | |
csv_out.writerow(sum([reader.next() for reader in csv_readers], [])) | |
except (StopIteration, IOError): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment