The default value for colnames in .export_to_csv is rows.colnames. Make sure the values you are customizing have the same format as this attribute. Sometimes the fieldname arguments must specify the table with dot syntax as following:
"tablename.fieldname" The api documentation does not specify an option to customize the csv output headers. I belive the headers use that format in order to have a unified syntax when converting csv files to database records. An alternative would be to change the csv output with the standard python csv tools. On Mar 17, 5:42 am, Rey <[email protected]> wrote: > Hi there, > > I've spent hours and tried everything under the sun, I'm new to web2py > but loving the framework. > > I want to be able to set the exported csv header row titles to not be > "table.id, table.name etc." > but rather a proper label "ID, Name" etc. > > This is part of the code I'm using: > > *from gluon.contenttype import contenttype > > > ** response.headers['Content-Type'] = contenttype('.csv') > > ** response.headers['Content-disposition'] = 'attachment; > > filename=%s_database.csv' % ( > > ** request.now > > ** ) > > ** import csv, cStringIO > > ** s = cStringIO.StringIO()* > > *colnames=['id','name']* > > *records.export_to_csv_file(s, delimiter=',', quotechar='"', > > > quoting=csv.QUOTE_NONNUMERIC, colnames=colnames)* > > *return s.getvalue()* > > * > * > The error I keep getting is something to do with an "_extra" : > > *Traceback (most recent call last): > > > > > > > > > > > ** File "/Users/Rey/Dropbox/web2py/gluon/restricted.py", line 204, in > > restricted > > ** exec ccode in environment > > ** File > > "/Users/Rey/Dropbox/web2py/applications/example/controllers/account.py", > > line 187, in <module> > > ** File "/Users/Rey/Dropbox/web2py/gluon/globals.py", line 172, in > > <lambda> > > ** self._caller = lambda f: f() > > ** File > > "/Users/Rey/Dropbox/web2py/applications/example/controllers/account.py", > > line 183, in export > > ** records.export_to_csv_file(s, delimiter=',', quotechar='"', > > quoting=csv.QUOTE_NONNUMERIC, colnames=colnames) > > ** File "/Users/Rey/Dropbox/web2py/gluon/dal.py", line 6669, in > > export_to_csv_file > > ** row.append(record._extra[col]) > > ** File "/Users/Rey/Dropbox/web2py/gluon/dal.py", line 4440, in > > __getattr__ > > ** return self[key] > > ** File "/Users/Rey/Dropbox/web2py/gluon/dal.py", line 4431, in > > __getitem__ > > ** return dict.__getitem__(self, key) > > **KeyError: '_extra'* > > Version of Web2py I'm using is Version 1.99.4 (2011-12-14 14:46:14) stable > > Any help would be much appreciated!

