OLD | NEW |
---|---|
(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 usable json | |
7 """ | |
qyearsley
2016/07/18 22:46:30
A few more little comments:
- I personally prefer
| |
8 | |
9 import argparse | |
10 import csv | |
11 import json | |
12 import sys | |
13 | |
14 | |
15 def main(argv): | |
16 parser = argparse.ArgumentParser() | |
17 parser.add_argument('filename', metavar='filename', | |
18 help='the path to the input CSV file.') | |
qyearsley
2016/07/18 22:46:30
Capitalize "The path to..."
| |
19 parser.add_argument('-o', '--output', | |
20 help='The output file name.') | |
21 convert_csv_to_json(argv[0]) | |
qyearsley
2016/07/18 22:46:31
I think here you might want to do something like:
| |
22 | |
23 | |
24 def convert_csv_to_json(filename, output_filename=None): | |
25 out = output_filename or (filename + '.json') | |
26 dict_list = [] | |
27 json_file = open(out, 'w') | |
28 with open(filename) as csv_file: | |
29 reader = csv.DictReader(csv_file) | |
30 for row in reader: | |
31 dict_list.append(row) | |
qyearsley
2016/07/18 22:46:31
If we want to apply some transformation on each ro
| |
32 json.dump(dict_list, json_file, indent=4) | |
33 | |
34 | |
35 if __name__ == '__main__': | |
36 main(sys.argv[1:]) | |
OLD | NEW |