import csv
datafile = csv.reader(open('datafile.csv','rb'), delimiter=",", quotechar='|')
date, data1, data2, data3 = [], [], [], []
for row in datafile:
date.append(row[0])
data1.append(row[1])
data2.append(row[2])
data3.append(row[3])
Here is what I want to do. As you can see, this code takes 1 csv file and creates 4 lists from it. Now I can and will do math on those lists, like data1[4]-data2[30]
But I also have a few other files that I also want to create lists from. But I want to be able to reuse my code like my math seen above ( data1[4]-data2[30] ). So ideally everything including the arrays should be named the same for the other files.
I am super new to programming so this is proving to be a bit difficult. Obviously things will clash with everything being named the same and I don’t want that. So somehow via oop I want to be able to re-use my math logic and not have to copy some massive math algorithms and renaming everything.
Obviously I don’t want to just do math on these 4 arrays, save the results and change file name. Nope everything has to work at the same time inside my program.
I’m hoping I can get some tips on how to do this. I’m trying to widen my scope of knowledge about how oop works but its proving to be difficult. I’m thinking getting an explanation on how to fix my own problem would be best.
So to recap, I have 4 lists created from 1 file. I want to create 4 additional lists from a different file but have the arrays be named the same. And then I only have to create my massive math calculations once and have it work for everything. And have it work for any future new data introduced into my program.
You want a data container, you get this by making a class with an appropriate constructor (init method here). Below is an example where you load data from two files.
If you want to apply methods (the math stuff you describe) you might want to put these methods in the class, can you give some more concrete examples of exactly what you want to do?