简体   繁体   English

django csv标头和行格式问题

[英]django csv header and row format problem

I am trying to create csv download ,but result download gives me in different format 我正在尝试创建csv下载,但结果下载给了我不同的格式

def csv_download(request):
    import csv
    import calendar
    from datetime import *
    from dateutil.relativedelta import relativedelta

    now=datetime.today()
    month = datetime.today().month
    d = calendar.mdays[month]

    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename=somefilename.csv'
    m=Product.objects.filter(product_sellar = 'jhon')
    writer = csv.writer(response)
    writer.writerow(['S.No'])
    writer.writerow(['product_name'])
    writer.writerow(['product_buyer'])
    for i in xrange(1,d):

       writer.writerow(str(i) + "\t")



    for f in m:
         writer.writerow([f.product_name,f.porudct_buyer])

    return response

output of above code : 上面代码的输出:

product_name
1
2
4
5
6
7
8
9
1|10
1|1
1|2
.
.
.
2|7


mgm | x_name
wge | y_name

I am looking out put like this 我看起来像这样

s.no   porduct_name product_buyser  1     2   3   4   5   6   7   8 9 10 .....27 total
  1   mgm            x_name         2      3      8                                13
  2  wge             y_name                   4       9                            13

can you please help me with above csv download ? 您能帮我以上csv下载吗? if possible can you please tell me how to sum up all the individual user total at end? 如果可能的话,能否请您告诉我最后如何总结所有个人用户总数?

Example : 范例:

we have selling table in that every day seller info will be inserted 我们有销售表,每天都会插入卖家信息

table data looks like 表数据看起来像

 S.no product_name product_seller sold Date
  1     paint        jhon           5   2011-03-01
  2     paint        simth          6   2011-03-02 

I have created a table where it displays below format and i am trying to create csv download 我创建了一个表格,其中显示以下格式,我正在尝试创建csv下载

s.no prod_name   prod_sellar 1-03-2011  2-03-2011   3-03-2011   4-03-2011 total
    1     paint         john       10        15               0               0     25
    2     paint         smith      2          6               2               0     10

Please read the csv module documentation , particularly the writer object API . 请阅读csv模块文档 ,尤其是writer对象API

You'll notice that the csv.writer object takes a list with elements representing their position in your delimited line. 您会注意到,csv.writer对象使用一个列表,其中包含表示其在分隔行中位置的元素。 So to get the desired output, you would need to pass in a list like so: 因此,要获得所需的输出,您需要像这样传递一个列表:

writer = csv.writer(response)
writer.writerow(['S.No', 'product_name', 'product_buyer'] + range(1, d) + ['total'])

This will give you your desired header output. 这将为您提供所需的标题输出。

You might want to explore the csv.DictWriter class if you want to only populate some parts of the row. 如果您只想填充行的某些部分,则可能要探索csv.DictWriter类。 It's much cleaner. 干净得多。 This is how you would do it: 这是您的操作方式:

writer = csv.DictWriter(response, 
                        ['S.No', 'product_name', 'product_buyer'] + range(1, d) + ['total'])

Then when your write command would follow as so: 然后,当您的write命令将如下所示时:

for f in m:
    writer.writerow({'product_name': f.product_name, 'product_buyer': f.product_buyer})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM