I'm new to python and not very fond of Object oriented programming. I'm trying to figure out the best way to achieve the following:
I want to read a file like this: INPUT from file:
Name,semester,point
abc,5,3.5
cba,2,3.5
I want to store it in a dictionary like the name as a primary key and the other two values it is related to. as in,
OUTPUT maybe:
abc:'5','3.5'
I need to use my stored value for some calculations in other functions. So as long as I store them and can access them in some way it is alright. It can be list, arraylist, dictionary or whatever seems like a good solution.
From what I read, I want to use Methods and Instances instead of class and objects. I think that sounds about right from what I understand.
Please suggest the best way to achieve this, as I have already checked stackoverflow for similar questions and I have only found solutions that are object oriented (which I want to avoid).
I have used the example by @Ajax1234 above. Seems like a good solution and example.
From what I understand the confusion is between Instances and Classes and their relation to Methods
Correct me if I'm wrong in explaining this.
import sys
filename = sys.argv[1]
class Data: # class
def __init__(self, filename):
self.data = {} #will store data in empty dictionary
self.filename = filename
# funtion in class as attribute can be a METHOD
def get_data(self):
return [i.strip('\n').split(',') for i in open(filename)]
# funtion in class as attribute can be a METHOD
def set_data(self):
for i in self.get_data():
self.data[i[0]] = i[1:]
# Instances are derived from Class
# file_data is an Instance created from the class Data
file_data = Data(filename)
# method called through Instance
file_data.set_data()
# will print the full dictionary
print(file_data.data)
Reference: Learning Python by Mark Lutz
Hope this explains it.