简体   繁体   中英

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. 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. 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. 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).

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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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