| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Converts CSV files to machine-readable JSON. | 6 """Converts CSV files to machine-readable JSON. |
| 7 | 7 |
| 8 Example usage: | 8 Example usage: |
| 9 | 9 |
| 10 ./csv_to_json input.csv --output directory_owners.json\ | 10 ./csv_to_json input.csv --output directory_owners.json\ |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 convert_csv_to_json(args.filename, args.output, args.field_names, args.skip_
keys) | 32 convert_csv_to_json(args.filename, args.output, args.field_names, args.skip_
keys) |
| 33 | 33 |
| 34 | 34 |
| 35 def convert_csv_to_json(filename, output_filename=None, field_names=None, skip_k
eys=None): | 35 def convert_csv_to_json(filename, output_filename=None, field_names=None, skip_k
eys=None): |
| 36 out = output_filename or (filename + '.json') | 36 out = output_filename or (filename + '.json') |
| 37 dict_list = [] | 37 dict_list = [] |
| 38 json_file = open(out, 'w') | 38 json_file = open(out, 'w') |
| 39 with open(filename) as csv_file: | 39 with open(filename) as csv_file: |
| 40 reader = csv.DictReader(csv_file, fieldnames=field_names) | 40 reader = csv.DictReader(csv_file, fieldnames=field_names) |
| 41 for row in reader: | 41 for row in reader: |
| 42 if not row['directory'].startswith('imported'): | 42 if not row['directory'].startswith('external'): |
| 43 continue | 43 continue |
| 44 if skip_keys: | 44 if skip_keys: |
| 45 for s in skip_keys: | 45 for s in skip_keys: |
| 46 del row[s] | 46 del row[s] |
| 47 dict_list.append(row) | 47 dict_list.append(row) |
| 48 json.dump(dict_list, json_file, indent=4) | 48 json.dump(dict_list, json_file, indent=4) |
| 49 | 49 |
| 50 | 50 |
| 51 if __name__ == '__main__': | 51 if __name__ == '__main__': |
| 52 main(sys.argv[1:]) | 52 main(sys.argv[1:]) |
| OLD | NEW |