Re: Confused newbie needs help with "__init__() takes exactly 11 arguments (1 given)"
d'oh I'm an idiot... you are making a 'list' object. -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused newbie needs help with "__init__() takes exactly 11 arguments (1 given)"
it looks like your problem is in this line: reviews = [Review(*[field.strip() for field in row]) for row in reader] ouch! split that up a bit so we can understand what the heck you are trying to do here. Also, it appears the whole thing is in these [ ] ? why? -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused newbie needs help with "__init__() takes exactly 11 arguments (1 given)"
googleboy wrote: > I've written a little script to parse a csv file then use seach/replace > over a template to create a file for each line in the file. It pikes > out when I call the function that parses the csv (read_revs). If I > have inadvertantly left an extra comma or two in the comma field, it > gives an error that says: > > Traceback (most recent call last): > File > "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", > line 310, in RunScript > exec codeObject in __main__.__dict__ > File "d:\dev\python\projects\books\revgen3.py", line 177, in ? > gen_html() > File "d:\dev\python\projects\books\revgen3.py", line 92, in gen_html > revlist = > read_revs(r'D:\library\work\websites\gobooks\cgi\reviews.csv') > File "d:\dev\python\projects\books\revgen3.py", line 38, in read_revs > reviews = [Review(*[field.strip() for field in row]) for row in > reader] > TypeError: __init__() takes exactly 11 arguments (13 given) > > Which I understand totally, because I can see the two extra fields in > the csv. Problem is, if I correct that problem it gives me confusing > output that is identical except for the final line which says: How do you "correct" that problem? > > TypeError: __init__() takes exactly 11 arguments (1 given) > > I don't get how it goes from 13 arguments to 1! I now have 10 fields > and can understand that it is passing self as the 11th argument. > > Particularly confusing to me was that when I translated this from a > windows box running Python 2.4 to an OpenBSD box running python 2.3, it > just worked on the unix box. > > Find below an the code and an example of the csv file in question. > > TIA > > googleboy > > > > > - > param1,param2,param3,param4,param5,param6,param7,param8,param9,comments > data1,data2,data3,data4,data5,data6,data7,data8,data9,Comments on the > item reviewed go here. Sometime, but not all the time, there might be > extra commas in this text. > data1,data2,data3,data4,data5,data6,data7,data8,data9,Comments on the > item reviewed go here. > > > > -- > > import string, os, re, sys, operator, csv > > class Review(object): > def __init__(self, param1, param2, param3, param4, param5, param6, > param7, param8, param9, comments): > params = locals() > del params['self'] > self.__dict__.update(params) > def __repr__(self): > all_items = self.__dict__.items() > return '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s' % (self.param1, > self.param2, self.param3, self.param4, self.param5, self.param6, > self.param7, self.param8, self.param9, self.comments) > > > def read_revs(filename): > csv_file = open(filename, "rb") > reader = csv.reader(csv_file) > reviews = [Review(*[field.strip() for field in row]) for row in > reader] so that you can see what you are actually getting as the 1 argument instead of guessing, try some diagnostics: reviews = [] for row in reader: args = [field.strip() for field in row] print "row", repr(row) print "args", repr(args) reviews.append(Review(*args)) > csv_file.close() > return reviews > > > def gen_revs(): > > revlist = read_revs(r'd:\dev\python\projects\books\reviews.csv') > revheader = revlist[0] > all_reviews = revlist[1:] > > template = open(r'd:\dev\python\projects\books\template.txt', 'r') > sTemplate = template.read() > > for review in all_reviews: > param1 = getattr(review, 'param1') > param2 = getattr(review, 'param2') > param3 = getattr(review, 'param3') > param4 = getattr(review, 'param4') > param5 = getattr(review, 'param5') > param6 = getattr(review, 'param6') > param7 = getattr(review, 'param7') > param9 = getattr(review, 'param8') > param9 = getattr(review, 'param9') > comments = getattr(review, 'comments') > > output = template % (param1, param2, param3, param4, param5, > param6, param7, param8, param9, comments) > > f=open(r"d:\dev\python\projects\books\%s.html" % param1, 'w') > f.write(output) > f.close() > -- http://mail.python.org/mailman/listinfo/python-list
Confused newbie needs help with "__init__() takes exactly 11 arguments (1 given)"
Mostly I posted to try to find out more about the arguments being passed to __init__, and why they'd be 13 one second and 1 the next. I also was hoping to get a little more background on what is happening under the hood of the csv function I've plagiarised. I haev read the online python doco on it, but I am not really entirely sure in my own head how it is breaking down that csv entry. I am actually quite familiar with handling strings and lists in python, though less so with dictionaries. I did like your trick with telling splut to stop after 9 times. I hadn't seen that before. Thanks for the response. :-) Googleboy -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused newbie needs help with "__init__() takes exactly 11 arguments (1 given)"
That's great, but it seems like a lot of work just to read in everything. Take a look at the sample that I did with the interpreter... First, X is a simulated input line... --- x = " param1,param2,param3,param4,param5,param6,param7,param8,param9,comments blah , blah, test" --- Ok, so just like your first example, with commas in the comments. Next, let's parse that into the variables and seperate on the commas... without using the CSV module.. --- param1, param2, param3, param4, param5, param6, param7, param8, param9, comments = x.split(',',9) --- Simply, this splits on the commas, up to 9 times. So, no need to fix your input text. Now, the output... --- f.write(param1 + " " + param2 + " " + param3 + " " + param4 + " " + param5 + " " + param6 + " " + param7 + " " + param8 + " " + param9 + " " + "\"" comments + "\"" + "\n") --- Writes the file, no commas except in the comments, and the comments are enclosed in quote marks. Also, the line ends in a CR+LF (depending on O/S). Is that something similar to what you were looking for? It doesnt look like your file is properly formatted for CSV, hence I suggest not using the CSV module. If you want to make your file CSV compatible, it should have quotation marks around the comments, as they could include commas. -- http://mail.python.org/mailman/listinfo/python-list
Confused newbie needs help with "__init__() takes exactly 11 arguments (1 given)"
I've written a little script to parse a csv file then use seach/replace over a template to create a file for each line in the file. It pikes out when I call the function that parses the csv (read_revs). If I have inadvertantly left an extra comma or two in the comma field, it gives an error that says: Traceback (most recent call last): File "C:\dev\python\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript exec codeObject in __main__.__dict__ File "d:\dev\python\projects\books\revgen3.py", line 177, in ? gen_html() File "d:\dev\python\projects\books\revgen3.py", line 92, in gen_html revlist = read_revs(r'D:\library\work\websites\gobooks\cgi\reviews.csv') File "d:\dev\python\projects\books\revgen3.py", line 38, in read_revs reviews = [Review(*[field.strip() for field in row]) for row in reader] TypeError: __init__() takes exactly 11 arguments (13 given) Which I understand totally, because I can see the two extra fields in the csv. Problem is, if I correct that problem it gives me confusing output that is identical except for the final line which says: TypeError: __init__() takes exactly 11 arguments (1 given) I don't get how it goes from 13 arguments to 1! I now have 10 fields and can understand that it is passing self as the 11th argument. Particularly confusing to me was that when I translated this from a windows box running Python 2.4 to an OpenBSD box running python 2.3, it just worked on the unix box. Find below an the code and an example of the csv file in question. TIA googleboy - param1,param2,param3,param4,param5,param6,param7,param8,param9,comments data1,data2,data3,data4,data5,data6,data7,data8,data9,Comments on the item reviewed go here. Sometime, but not all the time, there might be extra commas in this text. data1,data2,data3,data4,data5,data6,data7,data8,data9,Comments on the item reviewed go here. -- import string, os, re, sys, operator, csv class Review(object): def __init__(self, param1, param2, param3, param4, param5, param6, param7, param8, param9, comments): params = locals() del params['self'] self.__dict__.update(params) def __repr__(self): all_items = self.__dict__.items() return '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s' % (self.param1, self.param2, self.param3, self.param4, self.param5, self.param6, self.param7, self.param8, self.param9, self.comments) def read_revs(filename): csv_file = open(filename, "rb") reader = csv.reader(csv_file) reviews = [Review(*[field.strip() for field in row]) for row in reader] csv_file.close() return reviews def gen_revs(): revlist = read_revs(r'd:\dev\python\projects\books\reviews.csv') revheader = revlist[0] all_reviews = revlist[1:] template = open(r'd:\dev\python\projects\books\template.txt', 'r') sTemplate = template.read() for review in all_reviews: param1 = getattr(review, 'param1') param2 = getattr(review, 'param2') param3 = getattr(review, 'param3') param4 = getattr(review, 'param4') param5 = getattr(review, 'param5') param6 = getattr(review, 'param6') param7 = getattr(review, 'param7') param9 = getattr(review, 'param8') param9 = getattr(review, 'param9') comments = getattr(review, 'comments') output = template % (param1, param2, param3, param4, param5, param6, param7, param8, param9, comments) f=open(r"d:\dev\python\projects\books\%s.html" % param1, 'w') f.write(output) f.close() -- http://mail.python.org/mailman/listinfo/python-list