Harri
Harri

Reputation: 7

formulating a class to bring in new data while referencing a dictionary

I have:

if you wanted to accomplish this with classes instead of functions so you could import a csv and run it on new data.

Which class would you make first and how would you iterate through the class to compare each piece of data as a part is in every building with different quantities but the mass of that data is in a file

Appreciate your help.. sorry for vagueness just want to see if there are any suggestions!

part = [1,2,3,4,5]
building = [1,2,3,4,5]
qty = [1,2,3,4,5]

Upvotes: 0

Views: 34

Answers (1)

Charles Landau
Charles Landau

Reputation: 4265

You would just make the dictionary a data member of the class

class Container:
    def __init__(self):
        self.data = {"part": [], # Data member of class
                     "building": [],
                     "qty": []}

    # Pass self to method of class, so it can access data members
    def ingest_csv(self, filepath): 
        with open(filepath, "r") as file:
            for line in file[1:]:
                part, building, qty = line.split(",")
                self.data["part"].append(part)
                self.data["building"].append(building)
                self.data["qty"].append(qty)

edit: then to use it you would do the following.

container = Container()
container.ingest_csv("./path/to.csv")

# Print the dict if you want to view it
print(container.data)

Upvotes: 1

Related Questions