Created
January 14, 2010 10:23
-
-
Save j4mie/277051 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
# Concatenate a set of CSV files passed as arguments. The headers of all | |
# the files must match. Output will be a single header row, followed | |
# by all the rows from all the CSV files. | |
import sys | |
if len(sys.argv) == 1: | |
sys.exit("No arguments supplied") | |
buffer = [] | |
headers = "" | |
for arg in sys.argv[1:]: | |
file_lines = open(arg).readlines() | |
if len(file_lines) == 0: | |
sys.exit("Empty file: %s" % arg) | |
if headers == "": | |
headers = file_lines[0] | |
elif headers != file_lines[0]: | |
sys.exit("Headers do not match in file: %s" % arg) | |
buffer += file_lines[1:] | |
print headers, ''.join(buffer) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment