Confused newbie needs help with "__init__() takes exactly 11 arguments (1 given)"

G

googleboy

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()
 
S

sp1d3rx

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.
 
J

John Machin

googleboy said:
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()
 
S

sp1d3rx

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top