Solved

Python csv creating extra carriage return

Posted on 2011-03-22
8
2,026 Views
Last Modified: 2012-05-11
I'm creating a comma delimited file using Python's csv module.  I'm writing rows by using the csvwriter.writerow(row)

When I do that and open the resulting file in MS Excel, I'm getting an extra blank line in between each row.  Looking at the hex of the file, there are two carriage returns and a line feed CR CR LF at the end of every row.

How can I stop the extra carriage return when using writerow() ?  

I'm certain my data does not contain a carriage return.  Also, when I run the same application under linux, the extra carriage return is not there and I'm able to open the file in Excel without problem.

I'm using Python 2.6.

Thanks.
0
Comment
Question by:JohnSantaFe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 35193857
We would have to see the code you use to write your output.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 35193885
Suppose it is contained in variable s
Use the following, to explicitly remove it.
s.replace('\n', '')
0
 

Author Comment

by:JohnSantaFe
ID: 35194535
The attached code is an example.  It creates the following output:

0,1,2,3,4,5,6,0

1,2,3,4,5,6,7,0

2,3,4,5,6,7,8,0

3,4,5,6,7,8,9,0

4,5,6,7,8,9,10,0

5,6,7,8,9,10,11,0

6,7,8,9,10,11,12,0

7,8,9,10,11,12,13,0

8,9,10,11,12,13,14,0

9,10,11,12,13,14,15,0


file_name1 = 'my_data.csv'
leu1_file = open(file_name1, 'w')
leu1_writer = csv.writer(leu1_file)
       entries = [0]*8
        for x in range(10):   #10 rows
            for y in range(7):  #create a row
                entries[y] = y + x
            leu1_writer.writerow(entries) #write the row

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Accepted Solution

by:
clockwatcher earned 350 total points
ID: 35194861
You need to open the file you're writing in binary ('b') mode.

import csv
file_name1 = 'my_data.csv'
leu1_file = open(file_name1, 'wb')
leu1_writer = csv.writer(leu1_file)
entries = [0]*8
for x in range(10):   #10 rows
    for y in range(7):  #create a row
        entries[y] = y + x
    leu1_writer.writerow(entries) #write the row

Open in new window

0
 
LVL 29

Expert Comment

by:pepr
ID: 35197376
... and you should also close the file at the end.

leu1_file.close()
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 150 total points
ID: 35198611
As clockwatcher wrote.  The explanation is that when you write to a file opened in the text mode on Windows, the LF (the sequence \n) is automatically converted to CR LF (\r\n).  But the csv writer does not know how the file was opened. It assumes binary mode and sends CR LF.  Because of that you get CR (the first one, not converted) plus CR LF (from the converted LF).
0
 

Author Comment

by:JohnSantaFe
ID: 35204299
Changing to wb worked.  I wish the Python documentation had addressed the issue.
Also, the assumption that the file was opened as binary for a CSV file I think is a bad one.
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 35204391
The python documentation (http://docs.python.org/library/csv.html) does mention it:

csv.writer(csvfile[, dialect='excel'][, fmtparam])¶

    Return a writer object responsible for converting the user’s data into delimited strings on the given file-like object. csvfile can be any object with a write() method. If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question