Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Side by Side Diff: third_party/WebKit/Tools/Scripts/csv_to_json

Issue 2155523002: Add mapping of directories to owners as json file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: correction Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Tools/Scripts/webkitpy/w3c/directory_owners.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright 2016 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Converts CSV files to machine-readable JSON."""
qyearsley 2016/07/19 22:17:28 I think it would be helpful give an example invoca
7
8 import argparse
9 import csv
10 import json
11 import sys
12
13
14 def main(argv):
15 parser = argparse.ArgumentParser()
16 parser.add_argument('filename', metavar='filename',
17 help='The path to the input CSV file.')
18 parser.add_argument('-o', '--output',
19 help='The output file name.')
20 parser.add_argument('-f', '--fieldnames', nargs='*',
qyearsley 2016/07/19 22:17:28 fieldnames -> field-names?
21 help='The ordered fieldnames of the CSV file. Defaults t o first row.')
qyearsley 2016/07/19 22:17:28 fieldnames -> field names
22 parser.add_argument('-s', '--skip-keys', nargs='*',
23 help='Fields that should be skipped.')
24 args = parser.parse_args()
25 convert_csv_to_json(args.filename, args.output, args.fieldnames, args.skip_k eys)
26
27
28 def convert_csv_to_json(filename, output_filename=None, field_names=None, skip_k eys=None):
29 out = output_filename or (filename + '.json')
30 dict_list = []
31 json_file = open(out, 'w')
32 with open(filename) as csv_file:
33 reader = csv.DictReader(csv_file, fieldnames=field_names)
qyearsley 2016/07/19 22:17:28 fieldnames -> field_names
34 for row in reader:
35 if skip_keys:
36 for s in skip_keys:
37 del row[s]
38 dict_list.append(row)
39 json.dump(dict_list, json_file, indent=4)
40
41
42 if __name__ == '__main__':
43 main(sys.argv[1:])
qyearsley 2016/07/19 22:17:28 four-space indent
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Tools/Scripts/webkitpy/w3c/directory_owners.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698