简体   繁体   English

使用python在csv中一行编写多个字段

[英]Writing several fields in one row in a csv with python

I'm working on improving some open-source code for a psychological test. 我正在为心理测试改进一些开源代码。 For an output of data, I'm using python's CSV module, which works fine in itself. 对于数据输出,我正在使用python的CSV模块,该模块本身可以正常工作。 The data output is dependent on the Django model (trials) and the respective database entry and I want to add the data from another model (participant) to the CSV. 数据输出取决于Django模型(试用版)和相应的数据库条目,我想将来自另一个模型(参与者)的数据添加到CSV中。 So far I only managed to add the required entries in a new row but this basically renders the CSV unusable for further use in statistics programs. 到目前为止,我仅设法在新行中添加了所需的条目,但这基本上使CSV无法在统计程序中进一步使用。 In short what I get: 简而言之,我得到了:

headers of database entry 1
headers of database entry 2
values of database entry 1
values of database entry 2

But what I want is the output to look like this: 但是我想要的是输出看起来像这样:

headers of database entry 1 followed  by 2
values of database entry 1 followed by 2

This is what the important part of the export looks like. 这就是出口的重要部分。

def export_as_csv(modeladmin, request, queryset):
    headers, fields = zip(*export_info_trial)
    headers_participant, fields_participant = zip(*export_info_participant)
    zipFile = ZipFile(in_memory, 'w')
    for participant in queryset:                        
        rows = [headers_participant] + [headers] 
        participants = Participant.objects.filter(id=participant.id)
        trials = Trial.objects.filter(participant=participant.id)
        for participant_value_list in participants.values_list(*fields_participant):
            rows.append([unicode(v) for v in participant_value_list])
        for trial_value_list in trials.values_list(*fields):
            rows.append([unicode(v) for v in trial_value_list])

I do understand that the output as it is now is caused by me calling rows.append twice but right now I don't have any Idea how to combine these two calls cleverly (or at all). 我确实知道,现在的输出是由我两次调用rows.append引起的,但是现在我不知道如何巧妙地(或根本不)组合这两个调用。

edit: The writer is called as follows: 编辑:作者被称为如下:

f = StringIO()
writer = UnicodeWriter(f)                                               
for row in rows:
  writer.writerow(row)

I also added the preceding part of the function above. 我还添加了上面函数的前面部分。

I thank everyone for any help! 我感谢大家的帮助!

You can use writerow of csv.writer of python's CSV, like this : 您可以使用python CSV的csv.writer的writerow,如下所示:

def export_as_csv(modeladmin, request, queryset):
    headers, fields = zip(*export_info_trial)
    headers_participant, fields_participant = zip(*export_info_participant)
    zipFile = ZipFile(in_memory, 'w')
    rows = [headers_participant,headers] 
    result_row = []
    result_row.append(rows)
    for participant in queryset:                        
        row1 = []
        row2 = []
        participants = Participant.objects.filter(id=participant.id)    
        trials = Trial.objects.filter(participant=participant.id)               
        for participant_value_list in participants.values_list(*fields_participant):
            row1.append(unicode(v) for v in participant_value_list)
        for trial_value_list in trials.values_list(*fields):
            row2.append(unicode(v) for v in trial_value_list)
        row = row1 + row2
        result_row.append(row)

    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
    writer = csv.writer(response)                                           
    for row in result_row:
        writer.writerow(row)

By writerow , you will get a list inside result_row in the same row of CSV. 通过writerow ,您将在CSV的同一行中的result_row中获得一个列表。

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

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