Web2py - python

I am developing an application using web2py and I want to generate a csv file so that the columns become rows.

for example: The file is created as:

  name    mobile    email    Address
  yyy     yyyyy      yyy     yyyyy

      

I want the file to be generated as the following project:

name      yyy
mobile    yyyy
email     yyyyyy
Address   yyyy

      

How can i do this?

I used this code to generate a csv file:

import gluon.contenttype
response.headers['Content-Type'] = \
    gluon.contenttype.contenttype('.csv')
response.headers['Content-disposition'] = 'attachment; filename=members_approaching_renewal_report.csv'\   rows=db().select(db.member.membership_id,db.member.first_name,db.member.middle_name,db.member.last_name,db.member.birthdate,db.member.membership_status,db.member.registration_date,db.member.membership_end_date)
rows.colnames=('Membership Id','First Name','Middle Name','Last Name','Birthday Date','Membership Status','Registration Date','Membership ending Date')
return str(rows)

      

How can I change this code to do what I want?

+2


source to share


1 answer


If your results are in the named list of lists results

, and the title titles are in the named list headers

, you can transpose it like this:

transposed = zip(headers, *results)

      



Then output as usual with something like:

import csv
csv_writer = csv.writer(filename)
csv_writer.writerows(transposed)

      

+6


source







All Articles