[docs]defread_csv_to_dictionary(csvfile,delimiter=';'):"""Reads a csv file and returns a dictionary with the respective keys specified in the first row of the csv file. Parameters ---------- csvfile : str The path to csv file. delimiter : str, optional The character used to separate the values. Default ``;`` Returns ------- dict """data=[]withopen(csvfile,mode='r')asinfile:reader=csv.reader(infile,delimiter=delimiter)forrowsinreader:data.append(rows)infile.close()data=zip(*data)# transpose datadata_dict={}forcolindata:key=col[0]values=list(col[1:])data_dict.update({key:values})returndata_dict
[docs]defwrite_data_to_json(data,file):"""Write data to json file. Parameters ---------- data : dict, list, tuple, str, unicode, int, long, float, boolean, None The data to write to json file. Data must be JSON serialisable. file : str The path where to save the data. """withopen(file,'w')asf:json.dump(data,f)
[docs]defread_data_from_json(file):"""Read data from json file. Parameters ---------- file : str The path to the json file. Returns ------- object An object containing the deserialised data from the json file. """withopen(file)asf:data=json.load(f)returndata
[docs]defwrite_data_to_pickle(data,file):"""Write data to pickle file. Parameters ---------- data : object The data to write to a pickle file. file : str The path where to save the data. """withopen(file,'wb')asf:pickle.dump(data,f)
[docs]defread_data_from_pickle(file):"""Read data from pickle file. Parameters ---------- file : str The path to the pickle file. Returns ------- object An object containing the reconstituted object hierarchy. """withopen(file,'rb')asf:data=pickle.load(f)returndata