Solved

Python csv creating extra carriage return

Posted on 2011-03-22
8
1,856 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 28

Expert Comment

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

leu1_file.close()
0
 
LVL 28

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now