Revision 3071
Added by Aaron Marcuse-Kubitza over 12 years ago
cat_csv | ||
---|---|---|
1 | 1 |
#!/usr/bin/env python |
2 | 2 |
# Concatenates spreadsheets, removing any duplicated headers |
3 | 3 |
# Usage: self [sheet...] >out_sheet |
4 |
# |
|
5 |
# The existing header can be overwritten by adding a separate header file whose |
|
6 |
# header line is prefixed with "!". This is useful if the existing header's |
|
7 |
# column names are too long, not descriptive enough, etc. |
|
4 | 8 |
|
5 | 9 |
import os.path |
6 | 10 |
import sys |
... | ... | |
8 | 12 |
sys.path.append(os.path.dirname(__file__)+"/../lib") |
9 | 13 |
|
10 | 14 |
import csvs |
15 |
import strings |
|
11 | 16 |
import util |
12 | 17 |
|
13 | 18 |
def main(): |
14 | 19 |
paths = sys.argv[1:] |
15 | 20 |
|
21 |
header_written = False |
|
16 | 22 |
first_path = None |
17 | 23 |
first_info = None |
24 |
|
|
18 | 25 |
for path in paths: |
19 | 26 |
if path == '-': stream = sys.stdin |
20 | 27 |
else: stream = open(path, 'rb') |
... | ... | |
24 | 31 |
try: |
25 | 32 |
if info.dialect == None: continue # dialect of None = empty stream |
26 | 33 |
|
34 |
overwrite_ref = [False] # whether to overwrite the existing header |
|
35 |
info.header_line = strings.remove_prefix('!', info.header_line, |
|
36 |
overwrite_ref) |
|
37 |
|
|
27 | 38 |
def write_header(): sys.stdout.write(info.header_line) |
39 |
|
|
28 | 40 |
if first_info == None: |
29 | 41 |
first_path = path |
30 |
first_info = info |
|
31 |
write_header() |
|
42 |
if not overwrite_ref[0]: first_info = info |
|
43 |
# otherwise, next header becomes the compare-to header |
|
44 |
if not header_written: |
|
45 |
write_header() |
|
46 |
header_written = True |
|
32 | 47 |
else: |
33 | 48 |
if not util.classes_eq(info.dialect, first_info.dialect): |
34 | 49 |
raise SystemExit('Spreadsheet error: "'+path |
Also available in: Unified diff
cat_csv: Support overwriting the existing header using a separate header file