# You can copy this function into your code, or you can save this file in the same directory # as your data-analysis code, and import it as a library: # # from loadvectors import loadvectors # # Then you can use it to load data from CSV files: # # data = loadvectors("somefile.csv") # # Remember that this code will run on your computer, not on the ESP32 # It assumes that all of the data (aside from the column headers) is numerical. If you have # textual data, it will fail. def loadvectors(filename): """ Given the path to a CSV file with column headers, loads the file and returns a dict of lists, where the dict keys are the column headers and each corresponding value is a list with the data from that column. """ import csv # the Python csv library does the hard work for us reader = csv.DictReader(open(filename), skipinitialspace=True) d = dict() # Start with an empty dictionary for row in reader: # The reader will give us a dictionary for every line in the file for key,val in row.items(): # Iterate through the keys in the dictionary for this row if key in d: # If we've already got this key, d[key].append(float(val)) # just add the item to the end of the corresponding list else: # Otherwise, d[key] = [float(val)] # Create a new list with just this one element return d # Finally, return the completed dictionary