In: Computer Science
Python
I am creating a class in python. Here is my code below:
import csv
import json
population = list()
with open('PopChange.csv', 'r') as p:
reader = csv.reader(p)
next(reader)
for line in reader:
population.append(obj.POP(line))
population.append(obj.POP(line))
class POP:
""" Extract the data """
def __init__(self, line):
self.data = line
# get elements
self.id = self.data[0].strip()
self.geography = self.data[1].strip()
self.targetGeoId = self.data[2].strip()
self.targetGeoId2 = self.data[3].strip()
self.popApr1 = self.data[4].strip()
self.popJul1 = self.data[5].strip()
self.changePop = self.data[6].strip()
The problem is, I get an error saying:
population.append(obj.POP(line))
NameError: name 'obj' is not defined
What is causing this error? Is it a module I need to import or if I do need to assign obj to something, what am I supposed to assign it to?
What I am trying to do is create a program that allows a user to load a csv file and then print the statistics of the column in the csv chosen, such as the count, mean, standard deviation, min, max, and plot a histogram.
import csv import json population = list() with open('PopChange.csv', 'r') as p: reader = csv.reader(p) next(reader) for line in reader: population.append(POP(line)) population.append(POP(line)) class POP: """ Extract the data """ def __init__(self, line): self.data = line # get elements self.id = self.data[0].strip() self.geography = self.data[1].strip() self.targetGeoId = self.data[2].strip() self.targetGeoId2 = self.data[3].strip() self.popApr1 = self.data[4].strip() self.popJul1 = self.data[5].strip() self.changePop = self.data[6].strip()
************************************************** you can put the above code completely in a single .py file.. And run.. when you say, obj.POP, It means you are trying to use the POP class from an imported module obj. In this case, You have POP class defined locally, so you do not need to import anything from anyfile. Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.