diff --git a/src/file1.csv b/src/file1.csv new file mode 100644 --- /dev/null +++ b/src/file1.csv @@ -0,0 +1,4 @@ +id,name,age,height,weight +1,Alice,20,62,120.6 +2,Freddie,21,74,190.6 +3,Bob,17,68,120.0 diff --git a/src/file2.csv b/src/file2.csv new file mode 100644 --- /dev/null +++ b/src/file2.csv @@ -0,0 +1,4 @@ +Timestamp,Name,Age +4/11/2013,Raymond Herisse,22 +4/11/2013,Gil Collar,18 +4/12/2013,Stanley LaVon Gibson,43 diff --git a/src/testConvertCsvToJson.py b/src/testConvertCsvToJson.py new file mode 100755 --- /dev/null +++ b/src/testConvertCsvToJson.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +import csv +import json +import sys + +def usage(): + """ usage: ./test.py file_csv """ + print("Usage: " + sys.argv[0] + " file_csv") + sys.exit() + +def read_csv(input_file): + """print("lire fichier csv\n")""" + inputfile = open(input_file, "r") + reader = csv.reader(inputfile) + rownum = 0 + data = [] + for row in reader: + dic = {} + if rownum == 0: + header = row + else: + colnum = 0 + for col in row: + dic[header[colnum]] = col + colnum += 1 + data.append(dic) + rownum += 1 + inputfile.close() + return data + +def create_json(dic): + """print("creation fichier json\n")""" + """ FIXME: Parcourir le dictionnaire """ + """print (dic)""" + json_data = json.dumps(dic) + return json_data + + +def convert_csv_to_json(input_file): + dic = read_csv(input_file) + outputfile = open(input_file.split(".")[0] + ".json", "w") + outputfile.write(create_json(dic)) + outputfile.write('\n') + outputfile.close() + +""" Verifier nombre d'argument """ +""" Verifier que c'est un fichier et qu'il existe """ +if len(sys.argv) < 2: + usage() +""" fonction main: """ +convert_csv_to_json(sys.argv[1])