Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8907355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:53:41+00:00 2026-06-15T02:53:41+00:00

def export_csv(request): response = HttpResponse(mimetype = ‘text/csv’) response[‘Content-Disposition’] = ‘attachment; filename=exported.csv’ writer = csv.writer(response)

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:53:43+00:00Added an answer on June 15, 2026 at 2:53 am

    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]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I'm just serving files like this: # view callable def export(request): response =
def download_if_dne(href, filename): if os.path.isfile(filename): # print 'already downloaded:', href return False else: if
Created basic C++ DLL and exported names using Module Definition file (MyDLL.def). After compilation
Class UserController def export_users users = User.all stream_csv do |csv| csv << [Name,Email,Gender] users.each
I have a function in views.py def get_interview_type(request): i = None title = request.GET['title']
I'm trying to get a Criteria query to be exported to CSV, Excel, what
def self.jq_column_models COLUMN_NAME.collect {|x| {:name => x.to_s, :width => 80, :format => 'integer' if
def On_Instrumentation_StartAnimation(): Syntax : On_Instrumentation_StartAnimation() Purpose : Fired if the animation is started Parameters
def myFunc( a, b ): def innerFunc( c ): print c innerFunc( 2 )
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.