Last active
August 7, 2018 16:35
-
-
Save xlbruce/2fb44203f62726883fad3fa349a1e0b0 to your computer and use it in GitHub Desktop.
Convert reference prices in csv to json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import json | |
import sys | |
# Example csv file: | |
#originPlaceId;destinationPlaceId;travelCompanyId;serviceClassId;price;active;clientIds/0;clientIds/1;clientIds/2;clientIds/3;clientIds/4;clientIds/5 | |
#3827;3022;35;1;416.55;VERDADEIRO;1;2;3;10013;10014;10015 | |
try: | |
csv_filename = sys.argv[1] | |
except IndexError: | |
print("Usage: python {} csv_file".format(sys.argv[0])) | |
sys.exit(1) | |
def csv_to_dict(line): | |
obj = {} | |
def map_boolean(value): | |
return value == 'VERDADEIRO' | |
obj['originPlaceId'] = int(line[0]) | |
obj['destinationPlaceId'] = int(line[1]) | |
obj['travelCompanyId'] = int(line[2]) | |
obj['serviceClassId'] = int(line[3]) | |
obj['price'] = float(line[4]) | |
obj['active'] = map_boolean(line[5]) | |
obj['clientIds'] = list(map(int, line[6:])) | |
return obj | |
with open(csv_filename, 'r') as csv_file: | |
reader = csv.reader(csv_file, delimiter=';') | |
next(reader, None) # Skip CSV header | |
result = list(map(csv_to_dict, reader)) | |
print(json.dumps(result, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment