Skip to content

Instantly share code, notes, and snippets.

@j4mie
Created January 14, 2010 10:23
Show Gist options
  • Save j4mie/277051 to your computer and use it in GitHub Desktop.
Save j4mie/277051 to your computer and use it in GitHub Desktop.
# 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