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..a5d2e3c3d74a4d7a77568a9c98a3f8ff7b22aad9 |
--- /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 22:46:30
A few more little comments:
- I personally prefer
|
+ |
+import argparse |
+import csv |
+import json |
+import sys |
+ |
+ |
+def main(argv): |
+ parser = argparse.ArgumentParser() |
+ parser.add_argument('filename', metavar='filename', |
+ help='the path to the input CSV file.') |
qyearsley
2016/07/18 22:46:30
Capitalize "The path to..."
|
+ parser.add_argument('-o', '--output', |
+ help='The output file name.') |
+ convert_csv_to_json(argv[0]) |
qyearsley
2016/07/18 22:46:31
I think here you might want to do something like:
|
+ |
+ |
+def convert_csv_to_json(filename, output_filename=None): |
+ out = output_filename or (filename + '.json') |
+ dict_list = [] |
+ json_file = open(out, 'w') |
+ with open(filename) as csv_file: |
+ reader = csv.DictReader(csv_file) |
+ for row in reader: |
+ dict_list.append(row) |
qyearsley
2016/07/18 22:46:31
If we want to apply some transformation on each ro
|
+ json.dump(dict_list, json_file, indent=4) |
+ |
+ |
+if __name__ == '__main__': |
+ main(sys.argv[1:]) |