开发者

web2py - python

I'm developing an application using web2py, and I want to generate a csv file so that the columns becomes rows.

e.g: The file is generated as:

  name    mobile    email    Address
  yyy     yyyyy      yyy     yyyyy

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

name      yyy
mobile    yyyy
email     yyyyyy
Address   yyyy

How can I do this?

I used this code to generate the 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','M开发者_JAVA技巧iddle Name','Last Name','Birthday Date','Membership Status','Registration Date','Membership ending Date')
return str(rows)

How should I edit this code to make what I want?


If your results are in a list of lists called results, and your header titles are in a list called headers you can transpose it like this:

transposed = zip(headers, *results)

Then output as normal, with something like:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜