Chromium Code Reviews| Index: third_party/WebKit/Tools/Scripts/csv_to_json |
| diff --git a/third_party/WebKit/Tools/Scripts/csv_to_json b/third_party/WebKit/Tools/Scripts/csv_to_json |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..4de01c160f8aa0e81c2bb838d1bc819e8b4d6bf8 |
| --- /dev/null |
| +++ b/third_party/WebKit/Tools/Scripts/csv_to_json |
| @@ -0,0 +1,36 @@ |
| +#!/usr/bin/python |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| +"""Converts csv files to machine usable json |
|
qyearsley
2016/07/18 21:02:25
One blank line can be added above the docstring.
|
| + |
| +TODO(raikiri): DO NOT SUBMIT without a detailed description of csv_to_json. |
|
qyearsley
2016/07/18 21:02:25
This message can be removed.
|
| +""" |
| + |
| +import sys |
| +import csv |
| +import json |
| +import argparse |
|
qyearsley
2016/07/18 21:02:25
Imports should be sorted.
|
| + |
| +def main(argv): |
|
qyearsley
2016/07/18 21:02:25
Formatting note: usually there's two blank lines b
|
| + parser = argparse.ArgumentParser() |
| + parser.add_argument('filename', metavar='filename', |
| + help='the path to the csv') |
|
qyearsley
2016/07/18 21:02:25
the path to the input CSV file
|
| + parser.add_argument('-o', '--output', |
| + help='the output file name') |
|
qyearsley
2016/07/18 21:02:25
The help lines might look good if capitalized and
|
| + args = parser.parse_args(argv) |
|
qyearsley
2016/07/18 21:02:25
The argparse library can also access sys.argv by i
|
| + convert_csv_to_json(args.filename) |
| + |
| + |
| +def convert_csv_to_json(filename, output_filename=None): |
| + out = output_filename or (filename + '.json') |
| + dict_list = [] |
| + jsonfile = open(out, 'w') |
|
qyearsley
2016/07/18 21:02:25
jsonfile -> json_file
|
| + with open(filename) as csvfile: |
|
qyearsley
2016/07/18 21:02:25
csvfile -> csv_file
|
| + readerobj = csv.DictReader(csvfile) |
|
qyearsley
2016/07/18 21:02:25
readerobj -> reader
|
| + for row in readerobj: |
| + dict_list.append(row) |
| + json.dump(dict_list, jsonfile, indent=4) |
| + |
|
qyearsley
2016/07/18 21:02:25
We can have two blank lines between the last funct
|
| +if __name__ == '__main__': |
| + main(sys.argv[1:]) |