The CSV example in the book does it this way (http://web2py.com/book/default/chapter/09#CSV), but it doesn't explicitly mention the gotcha.
On Wednesday, November 16, 2011 4:57:04 PM UTC-5, Nate Atkinson wrote: > > Aha! Thank you so much. > > My first time writing a non-HTML view-- didn't realize the braces > generated a blank line, or that I could include multiple lines within > braces. You've solved the problem, and made my code look nicer too! > > Thanks > > Nate > > On Nov 16, 4:34 pm, Anthony <abas...@gmail.com> wrote: > > Actually, the number of blank lines is exactly 6 + (3 x number of rows). > In > > web2py templates, every {{...}} that doesn't write anything to the view > > still generates a blank line in the output. Your code (up to and > including > > the response.write line) includes 6 blocks of {{...}} that are called > once, > > plus three blocks of {{...}} (including pass) within your loop > (resulting > > in 3 blank lines per record). > > > > Instead, try putting all the code in a single block: > > > > {{ > > [all code goes here] > > > > }} > > > > Anthony > > > > > > > > > > > > > > > > On Wednesday, November 16, 2011 2:36:26 PM UTC-5, Nate Atkinson wrote: > > > > > Hi, > > > > > I'm trying to create a CSV view for a set of rows. Here's the code for > > > the view: > > > > > {{import StringIO, csv}} > > > {{outfile = StringIO.StringIO()}} > > > {{mywriter = csv.writer(outfile, dialect=csv.excel_tab)}} > > > {{mywriter.writerow(['Name', 'Address', 'City', 'State'])}} > > > {{for row in places:}} > > > {{mydata = [row.plads.name, row.plads.address, row.plads.city, > > > row.plads.state.state]}} > > > {{mywriter.writerow(mydata)}} > > > {{pass}} > > > {{response.headers['Content-Disposition'] = 'attachment; filename= > > > %s.csv' %(routeinfo.name)}} > > > {{response.write(outfile.getvalue(), escape=True)}} > > > {{outfile.close()}} > > > > > This generates a csv file, but when I open it with excel, there are > > > several blank lines before the usable data. The only rhyme or reason I > > > see to it, is that the number of leading blank lines is about 3.2x the > > > number of rows. So, when I have 30 rows (two different sets of 30 > > > rows), there are 96 leading blank lines. 27 rows gets me 87 leading > > > lines, and 19 rows gets me 63. > > > > > If I open the file with Emacs or try to import for MapPoint, I still > > > get the leading blank lines. If I open the file with Notepad, the > > > leading lines are not present. > > > > > I think this has something to do with the newline character. The > > > server is running Debian, and the clients are on Windows. > > > > > Anyone have any ideas for how to solve this?