Merge "Use py3 features in merge_csv.py." am: 3e6f701980 am: aad8e7b161

Change-Id: I7c1806c36a731ce823e45b2fd49cc7793ae50e3a
This commit is contained in:
Automerger Merge Worker
2020-01-31 16:30:19 +00:00
2 changed files with 36 additions and 7 deletions

View File

@@ -0,0 +1,30 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
python_binary_host {
name: "merge_csv",
main: "merge_csv.py",
srcs: ["merge_csv.py"],
version: {
py2: {
enabled: false,
},
py3: {
enabled: true,
embedded_launcher: true
},
},
}

View File

@@ -21,20 +21,19 @@ import csv
import sys
csv_readers = [
csv.DictReader(open(csv_file, 'rb'), delimiter=',', quotechar='|')
csv.DictReader(open(csv_file, 'r'), delimiter=',', quotechar='|')
for csv_file in sys.argv[1:]
]
# Build union of all columns from source files:
headers = set()
for reader in csv_readers:
headers = headers.union(reader.fieldnames)
headers = headers.union(reader.fieldnames)
# Concatenate all files to output:
out = csv.DictWriter(sys.stdout, delimiter=',', quotechar='|', fieldnames = sorted(headers))
out = csv.DictWriter(sys.stdout, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL,
dialect='unix', fieldnames=sorted(headers))
out.writeheader()
for reader in csv_readers:
for row in reader:
out.writerow(row)
for row in reader:
out.writerow(row)