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
Solved

Python csv creating extra carriage return

Posted on 2011-03-22
8
1,988 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stuck in average-1 12 65
Problem to line 23 53
Convert SuperCalc files to Excel 9 40
how to reset cell styles? 15 24
This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 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