extra rows in a CSV module output when viewed in excel 2007

J

JonathanB

The subject basically says it all, here's the code that's producing
the csv file:

def write2CSV(self,output):
writer = csv.writer(open(output, 'w'), dialect='excel')
writer.writerow(['Name','Description','Due Date','Subject',
'Grade','Maximum Grade', self.name,
self.grade])
for row in self.assignment_list:
writer.writerow(row.output2list())
return True

Here's the text from the csv file my test-run outputs:

Name,Description,Due Date,Subject,Grade,Maximum Grade,Test Student,2
Math Homework 8/21,Math Review pp. 103-104,2010-08-13,Math,10,10,,
Math Test,Chapter 5,2010-09-21,Math,45,50,,
Science Test,Matter,2010-09-11,Science,400,500,,

When I view it in Excel, there's an extra row between every result.
How can I remove those?
 
A

alex23

        writer = csv.writer(open(output, 'w'), dialect='excel')

I think - not able to test atm - that if you open the file in 'wb'
mode instead it should be fine.
 
J

JonathanB

I think - not able to test atm - that if you open the file in 'wb'
mode instead it should be fine.

changed that to
writer = csv.writer(open(output,'wb'),dialect='excel')

Now I get this error:

TypeError: must be bytes or buffer, not str

I'm using Python 3.1, maybe that changes things?
 
M

MRAB

JonathanB said:
changed that to
writer = csv.writer(open(output,'wb'),dialect='excel')

Now I get this error:

TypeError: must be bytes or buffer, not str

I'm using Python 3.1, maybe that changes things?

You want to open the file in text mode, but not write Windows line
endings (CRLF) for each newline:

writer = csv.writer(open(output, 'w', newline=''), dialect='excel')
 
J

JonathanB

You want to open the file in text mode, but not write Windows line
endings (CRLF) for each newline:

     writer = csv.writer(open(output, 'w', newline=''), dialect='excel')

That was it! Thank you, I knew it was something stupid. It's been so
long (6 months) since I coded, I forgot about how Windows/Mac mangle
line endings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top