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