pynovice
pynovice

Reputation: 7752

Don't include comma while writing to CSV in Django?

I am writing like this in Django:

writer.writerow(['VideoName ', 'Director ', 'Cameraman ', 'Editor ', 'Reporter ', 'Tag '])

It is writing in CSV like this:

response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="report.csv"'

Video, Director, Cameraman

But I only want:

Video Director Cameraman

I am doing this in Django.

Upvotes: 0

Views: 334

Answers (3)

Ricola3D
Ricola3D

Reputation: 2442

If I understand well, you don't want CSV format in your rows ?

You can writerow "".join(['VideoName ', 'Director ', 'Cameraman ', 'Editor ', 'Reporter ', 'Tag ']), it will write the VideoName Director Cameraman Editor Reporter Tag string as a line of you CSV file.

Or you can remove the spaces from your array entries, and specify the space character as CSV separator.

Upvotes: 0

falsetru
falsetru

Reputation: 369194

Specify delimiter when create csv.writer.

In addition to that trim field values. ('videoName ' -> 'videoName')

>>> import csv
>>> import sys
>>> 
>>> writer = csv.writer(sys.stdout, delimiter=' ')
>>> writer.writerow(['VideoName', 'Director', 'Cameraman', 'Editor', 'Reporter', 'Tag'])
VideoName Director Cameraman Editor Reporter Tag

Upvotes: 0

alecxe
alecxe

Reputation: 473943

Set a delimiter when you are initializing your csv.writer:

writer = csv.writer(buffer, delimiter=" ")

Upvotes: 2

Related Questions