blob: 6a5b0e1347b2426cda3ad27d9854c7e7b34980fa [file] [log] [blame]
Mathew Inwoodea14c0c2018-10-05 14:41:03 +01001#!/usr/bin/env python
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16"""
Artur Satayev4ef7a1a2020-01-20 19:09:06 +000017Merge multiple CSV files, possibly with different columns.
Mathew Inwoodea14c0c2018-10-05 14:41:03 +010018"""
19
Artur Satayev4ef7a1a2020-01-20 19:09:06 +000020import argparse
Mathew Inwoodea14c0c2018-10-05 14:41:03 +010021import csv
Artur Satayev4ef7a1a2020-01-20 19:09:06 +000022import io
Mathew Inwoodea14c0c2018-10-05 14:41:03 +010023
Artur Satayev4ef7a1a2020-01-20 19:09:06 +000024from zipfile import ZipFile
Mathew Inwoodea14c0c2018-10-05 14:41:03 +010025
Artur Satayev4ef7a1a2020-01-20 19:09:06 +000026args_parser = argparse.ArgumentParser(description='Merge given CSV files into a single one.')
27args_parser.add_argument('--header', help='Comma separated field names; '
28 'if missing determines the header from input files.')
29args_parser.add_argument('--zip_input', help='ZIP archive with all CSV files to merge.')
30args_parser.add_argument('--output', help='Output file for merged CSV.',
31 default='-', type=argparse.FileType('w'))
32args_parser.add_argument('files', nargs=argparse.REMAINDER)
33args = args_parser.parse_args()
34
35
36def dict_reader(input):
37 return csv.DictReader(input, delimiter=',', quotechar='|')
38
39
40if args.zip_input and len(args.files) > 0:
41 raise ValueError('Expecting either a single ZIP with CSV files'
42 ' or a list of CSV files as input; not both.')
43
44csv_readers = []
45if len(args.files) > 0:
46 for file in args.files:
47 csv_readers.append(dict_reader(open(file, 'r')))
48elif args.zip_input:
49 with ZipFile(args.zip_input) as zip:
50 for entry in zip.namelist():
51 if entry.endswith('.uau'):
52 csv_readers.append(dict_reader(io.TextIOWrapper(zip.open(entry, 'r'))))
53
Mathew Inwoodea14c0c2018-10-05 14:41:03 +010054headers = set()
Artur Satayev4ef7a1a2020-01-20 19:09:06 +000055if args.header:
56 fieldnames = args.header.split(',')
57else:
58 # Build union of all columns from source files:
59 for reader in csv_readers:
60 headers = headers.union(reader.fieldnames)
61 fieldnames = sorted(headers)
Mathew Inwoodea14c0c2018-10-05 14:41:03 +010062
63# Concatenate all files to output:
Artur Satayev4ef7a1a2020-01-20 19:09:06 +000064writer = csv.DictWriter(args.output, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL,
65 dialect='unix', fieldnames=fieldnames)
66writer.writeheader()
Mathew Inwoodea14c0c2018-10-05 14:41:03 +010067for reader in csv_readers:
Artur Satayev9430c172020-01-20 17:35:58 +000068 for row in reader:
Artur Satayev4ef7a1a2020-01-20 19:09:06 +000069 writer.writerow(row)