Hi. 

I have noticed that a few people have been frustrated with changing the 
colnames when exporting to cvs. 
I have arrived at a fairly compact solution, but perhaps not the most 
efficient one for large cvs dumps. 
For the record, and perhaps a bit of criticism, here it is  :-)


controller 

def foo(): 
    if request.extension in ('csv','tsv'): 
        rows = db().select(db.vw_traffic_by_time.ALL, 
orderby=db.vw_traffic_by_time.best_slot) 
        return dict(rows=rows) 
    .... 


view foo.tvs 

{{ 
import cStringIO 
stream=cStringIO.StringIO() 
colnames=map(lambda c: c.split('.')[1], rows.colnames) # get rid of the 
table part 
seek = len("\t".join(rows.colnames)+"\r\n") # possibly faster than 
readlines()[1:]? 
rows.export_to_csv_file(stream, delimiter="\t",null=0) 
stream.seek(seek) 
response.headers['Content-Type']='application/vnd.ms-excel' 
response.write("\t".join(colnames) + "\r\n", escape=False) 
response.write(''.join(stream.readlines()), escape=False) 
}} 

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to