aabujamra
aabujamra

Reputation: 4636

Writing string in CSV and getting multiple rows - Python

This must be very simple.

I'm trying to put a header in my csv file, the string 'Date,Value', as you can see below.

with open('ticker.csv', 'wb') as f:

writer = csv.writer(f)
writer.writerows('Date,Value')
writer.writerows(izip(dates, values))
f.close()

However I'm getting a multirowed string, like this:

D

a

t

e

","

V

a

l

u

e

2002-03,12.9

2002-04,12.5

2002-05,11.9

How can I fix that?

Upvotes: 0

Views: 505

Answers (2)

Kaushal Kumar Singh
Kaushal Kumar Singh

Reputation: 829

Here is the solution

writer = csv.writer(f)
writer.writerow(['Date','Value'])
writer.writerows(izip(dates, values))
f.close()

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599788

Use writerow - without the s - to write a single row.

Upvotes: 0

Related Questions