0
def export_csv(request):
    response = HttpResponse(mimetype = 'text/csv')
    response['Content-Disposition'] = 'attachment; filename=exported.csv'
    writer = csv.writer(response)
    data = [['First-Name','Last-name'],['Foo','baar']]
    for entry in data:
            writer.writerow(entry)
    return response

Above code is written for outputting csv file from django. Problem I am facing is the exported/outputted file's content are not displaying in different boxes in csv file editor(s). For each entry in data (list) it is getting printed in same box rather it should interpret each entry's value(First-Name, Last-Name) in different box.

Actual result in csv file:

|First-Name,Last-Name |
|Foor,bar             |

Expected:

|First-Name |Last-Name |
|Foo        |Baar      |

How can I get mechanism by which it will export the file independent on the list size and its contents?

4
  • 2
    The files you previously posted actually appear identical to me. One thing to keep in mind is that CSV's are literally just comma-separated values (as you see). It is actually the job of the editor you are using to interpret it correctly (for instance, when write to CSV and open in Excel, I will sometimes have to tell it to split on commas). Commented Nov 24, 2012 at 10:41
  • yes, it is working as you said, as I split file by comma it shows me expected result. I think there would be some way to display expected result in default environment rather saying csv editor to split the file with comma. Commented Nov 24, 2012 at 10:47
  • 1
    If you're using Excel, try creating the writer like writer = csv.writer(response, dialect='excel') - that may help in your case. Commented Nov 24, 2012 at 10:48
  • @Navid, where does does this csv file save to? What directory? I ran your code and I cannot find where the file was saved. Commented Aug 11, 2016 at 18:27

1 Answer 1

1

You could set your own dialect for csv.writer with your own custom delimiters etc :

class dia2(csv.Dialect):
    delimiter = '\t' # delimiter should be only 1-char
    quotechar = '"'
    escapechar = None
    doublequote = True
    skipinitialspace = False
    lineterminator = '\r\n'
    quoting = 1
writer = csv.writer(response, dialect=dia2)
...

The main cause of the problem with different editors different behaviour - that you should correctly set CSV-delimiter to one you really used - in each editor itself. One more possible reason - is that you don't use quoting for each CSV-value (that's quoting=1 in dialect class). One more thing you could expect, according to your expected output- the same length for each field, filling spaces for the rest - that couldn't be done through csv module, but you could format your data to the same length for each cell using standard python formats like:

data[i] = "%10s" % data[i]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.