Hammerstein
Hammerstein

Reputation: 13

Modify csv.writer code to write without blanks

Is there any way to modify the following code so that the resultant csv consists of consecutive entries of the list it's derived from, arranged vertically and separated only by line breaks?

with open('timeSeries.csv','w') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    wr.writerows((x,) for x in timeSeries)

This writes the list timeSeries into the csv as, for example:

3

5

6

When I'd prefer simply

3
5
6

Not sure what change exactly to make, though.

Upvotes: 1

Views: 51

Answers (2)

irahorecka
irahorecka

Reputation: 1807

I remember having this problem when using Windows...

Try adding newline="" as an argument to open. Does this help?

import csv

with open('timeSeries.csv','w',newline="") as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    wr.writerows((x,) for x in timeSeries)

Upvotes: 1

if I use this code I get a csv like you want but with quote then the problem is in timeSeries ?
"3"
"5"
"6"

import csv
timeSeries=[3,5,6]
with open('timeSeries.csv','w') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    wr.writerows((x,) for x in timeSeries)

Upvotes: 1

Related Questions