When I try to run this, it gave me this error. What did I do wrong?
Thanks
File "D:\Python26\my_module.py", line 25, in <module>
main()
File "D:\Python26\my_module.py", line 21, in main
for i in range(len(X.readColumn.names)):
AttributeError: 'function' object has no attribute 'names'Code:import csv class Reader(object): def __init__(self): print "Welcome" def readColumn(self,fileReader,fileReader_list=[],names=[],idigit=[]): self.fileReader = csv.reader(open("survey_result.csv", "rb")) self.fileReader_list = fileReader_list self.fileReader_list.extend(self.fileReader) self.names = names self.idigit = idigit for column in self.fileReader_list: self.names.append(column[1]) self.idigit.append(column[2]) del self.names[0] del self.idigit[0] self.names.sort() def main(): X = Reader() for i in range(len(X.readColumn.names)): print X.readColumn.names[i], X.readColumn.idigit[i] if __name__ == '__main__': main()
http://i3physics.com/blog
*-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*
You don't need to use "X.readColumn", when you declared names in self (self.names = names), that became a member of the object itself, not a local variable to the readColumn function. Do this:Code:for i in range(len(X.readColumn.names)): print X.readColumn.names[i], X.readColumn.idigit[i]
That should work.Code:for i, name in enumerate(X.names): print name, x.idigit[i]
Wow I changed my sig!
Hi, Zerk. Thanks for the reply.
I tried it but still no luck.
Traceback (most recent call last):Code:import csv class Reader(object): def __init__(self): pass def readColumn(self,fileReader,fileReader_list=[],names=[],idigit=[]): self.fileReader = csv.reader(open("survey_result.csv", "rb")) self.fileReader_list = fileReader_list self.fileReader_list.extend(self.fileReader) self.names = names self.idigit = idigit for column in self.fileReader_list: self.names.append(column[1]) self.idigit.append(column[2]) del self.names[0] del self.idigit[0] self.names.sort() X = Reader() for i in range(len(X.names)): print X.names[i], X.idigit[i]
File "D:\Python26\my_module.py", line 20, in <module>
for i in range(len(X.names)):
AttributeError: 'Reader' object has no attribute 'names'
http://i3physics.com/blog
*-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*
This is because you haven't ran the readColumn function yet. You'll need to run it or those values will not be added to the X reader object. You could add these in the constructor.
EDIT: Wait a tic...
What's this for?Code:del self.names[0] del self.idigit[0]
Wow I changed my sig!
Hi Zerk,
This script uses csv to read a csv data file. Each column is read as a list. That you know already. First row is the title of each individual column, so i delete them for my purpose.
I kinda got what you meant but I am still unable to run the script with your implementationCode:X = Reader() for i, names in enumerate(X.names): print names, X.idigit[i]
even with this
Code:for column in self.fileReader_list: names.append(column[1]) idigit.append(column[2]) del names[0] del idigit[0] names.sort() X = Reader() for i, names in enumerate(X.names): print names, X.idigit[i]
http://i3physics.com/blog
*-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*
Interesting name... "Zerk"...
Anyway, like I said, the readColumn object must be ran before those data members will be part of the X object. Python is not a statically typed language, further, you can simply add new members to objects even if those members don't ordinarily exist:
So if you want names and idigits to ALWAYS be members of the Reader object, you'll need to add them in the constructor:Code:X = MyObjectWithoutMemberPie() X.Pie = 5
Code:def __init__(self): self.names = [] self.idigit = []
Wow I changed my sig!
Hi Zeke
Sorry I am very careless.
Hmmm I think I got you in some ways, and this was fine
Thanks. I will continue to investigate writing this.Code:import csv class Reader(object): def __init__(self): self.names = [] self.idigit = [] self.fileReader_list=[] self.fileReader = csv.reader(open("survey_result.csv", "rb")) self.fileReader_list.extend(self.fileReader) for column in self.fileReader_list: self.names.append(column[1]) self.idigit.append(column[2]) del self.names[0] del self.idigit[0] X = Reader() for i, names in enumerate(X.names): print names, X.idigit[i]
http://i3physics.com/blog
*-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*
Hi, I just came across with a weird (well silly to expert :[ ) problem
This was my first attempt, and output as below:Code:import csv # let us first make student an object class Student(object): def __init__(self): #make sure we can access them self.fname = [] self.lname = [] self.gender = [] self.ID = [] #make sure we read it right away self.datalst = [] self.Reader = csv.reader(open("copy-john.csv", "rb")) self.datalst.extend(self.Reader) def Datahandler(self): for column in self.datalst: self.fname.append(column[0]) self.lname.append(column[1]) self.ID.append(column[2]) del self.fname[0],self.lname[0],self.ID[0] def Tableformat(self): print "%-14s|%-11s|%4s" %('First Name','Last Name','ID') print "-" * 39 for (i, fname) in enumerate(self.fname): print "%-14s|%-10s|%5s" %(fname,self.lname[i],self.ID[i]) X = Student() X.Tableformat()
You see, no data retrieved.>>> ================================ RESTART ================================
>>>
First Name |Last Name | ID
---------------------------------------
But if I removed this definition
def Datahandler(self):
and let codes below to move into __init__, then I could pull out the data. The code would work out as it was.
Why can't I separate them? If I have to, what should I do?
Thanks
Code below worked fine.
Code:import csv # let us first make student an object class Student(object): def __init__(self): #make sure we can access them self.fname = [] self.lname = [] self.gender = [] self.ID = [] #make sure we read it right away self.datalst = [] self.Reader = csv.reader(open("copy-john.csv", "rb")) self.datalst.extend(self.Reader) for column in self.datalst: self.fname.append(column[0]) self.lname.append(column[1]) self.ID.append(column[2]) del self.fname[0],self.lname[0],self.ID[0] def Tableformat(self): print "%-14s|%-11s|%4s" %('First Name','Last Name','ID') print "-" * 39 for (i, fname) in enumerate(self.fname): print "%-14s|%-10s|%5s" %(fname,self.lname[i],self.ID[i]) X = Student() X.Tableformat()
http://i3physics.com/blog
*-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*
I don't know if you really know what def does, it specifically defines functions, so what you have there is a nested function. Nested functions are perfectly legal in Python, and can be used to great effect when you need to perform a similar mathematical function repeatedly on different sets of data. Again, in Python you do not have to explicitly define variables before they're assigned to, you simply assign to them and they spring into existence as needed. Def is only really good to define functions, so unless you need a function, don't use it. Also, with nested functions they don't need to be passed self.
Second, whatever it is you're doing you're working too hard to do it. Python is more expressive than this, let me show you:
First, you don't need that Reader. Let's get rid of it:Code:#make sure we read it right away self.datalst = [] self.Reader = csv.reader(open("copy-john.csv", "rb")) self.datalst.extend(self.Reader) for column in self.datalst: self.fname.append(column[0]) self.lname.append(column[1]) self.ID.append(column[2]) del self.fname[0],self.lname[0],self.ID[0]
Second, you don't even need to have that datalst object. You can make a list on the fly by using the built-in list() function:Code:#make sure we read it right away self.datalst = [] self.datalst.extend(csv.reader(open("copy-john.csv", "rb"))) for column in self.datalst: self.fname.append(column[0]) self.lname.append(column[1]) self.ID.append(column[2]) del self.fname[0],self.lname[0],self.ID[0]
Finally, you don't need to delete that last stuff at the end of this. If you don't need the first rows, then simply exclude them from the for loop:Code:#make sure we read it right away for row in list(csv.reader(open("copy-john.csv", "rb"))): self.fname.append(row[0]) self.lname.append(row[1]) self.ID.append(row[2]) del self.fname[0],self.lname[0],self.ID[0]
There you have it. Simple, succinct, beautiful.Code:#make sure we read it right away for row in list(csv.reader(open("copy-john.csv", "rb")))[1:]: self.fname.append(row[0]) self.lname.append(row[1]) self.ID.append(row[2])
Wow I changed my sig!
Woo... Zeke. You are rock... I don't know how many years will it take me to learn all these things... Thanks a lot....
This is my final project and we only have two classes on python, so yeah... Thanks.
Any other suggestion so that I can become more expressive and confident with python? I know I am skipping around (that's the sad part).
http://i3physics.com/blog
*-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks