Question

In: Computer Science

Python I am creating a class in python. Here is my code below: import csv import...

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.

Solutions

Expert Solution

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.


Related Solutions

Python I am creating a program that allows a user to extract data from a .csv...
Python I am creating a program that allows a user to extract data from a .csv file and print the statistics of a certain column in that file. The statistics include Count, Mean, Standard Deviation, Min, and Max. Here is the code I have so far: import csv import json 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 =...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
This is my code for python. I am trying to do the fourth command in the...
This is my code for python. I am trying to do the fourth command in the menu which is to add an employee to directory with a new phone number. but I keep getting error saying , "TypeError: unsupported operand type(s) for +: 'dict' and 'dict". Below is my code. What am I doing wrong? from Lab_6.Employee import * def file_to_directory(File): myDirectory={}       with open(File,'r') as f: data=f.read().split('\n')    x=(len(data)) myDirectory = {} for line in range(0,199):      ...
this is my code in python I am trying to open a file and then print...
this is my code in python I am trying to open a file and then print the contents on my console but when i run it nothing prints in the console def file_to_dictionary(rosterFile): myDictionary={}    with open(rosterFile,'r') as f: for line in f: myDictionary.append(line.strip()) print(myDictionary)             return myDictionary    file_to_dictionary((f"../data/Roster.txt"))      
Here is my fibonacci code using pthreads. When I run the code, I am asked for...
Here is my fibonacci code using pthreads. When I run the code, I am asked for a number; however, when I enter in a number, I get my error message of "invalid character." ALSO, if I enter "55" as a number, my code automatically terminates to 0. I copied my code below. PLEASE HELP WITH THE ERROR!!! #include #include #include #include #include int shared_data[10000]; void *fibonacci_thread(void* params); void parent(int* numbers); int main() {    int numbers = 0; //user input....
UNSURE HOW TO PARSE: I am trying to parse a website with a CSV : import...
UNSURE HOW TO PARSE: I am trying to parse a website with a CSV : import csv import requests as r from bs4 import BeautifulSoup urltoget = 'a.ttu.edu/2019c/isqs6339/http://drd.bhw1/index.php' filepath = 'C:\\Users\\Zuliat\\Desktop\\test123.csv' res = r.get(urltoget) res.content if res.status_code == 200: print (' request is good') else: print (' bad request, retrieved code' + str(res.status_code))    print (res.content) soup = BeautifulSoup(res.content,'lxml') print(soup.title) They are responding with a message Line 1
Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
hi i do not know what is wrong with my python code. this is the class:...
hi i do not know what is wrong with my python code. this is the class: class Cuboid: def __init__(self, width, length, height, colour): self.__width = width self.__length = length self.__height = height self.__colour = colour self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height)) self.volume = height * length * width def get_width(self): return self.__width def get_length(self): return self.__length def get_height(self): return self.__height def get_colour(self): return self.__colour def set_width(self,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT