Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3442912
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:43:38+00:00 2026-05-18T08:43:38+00:00

Using Python to append CSV file, I get data every other row. How do

  • 0

Using Python to append CSV file, I get data every other row.
How do I fix?

import csv

LL = [(1,2),(3,4)]

Fn = ("C:\Test.csv")
w = csv.writer(open(Fn,'a'), dialect='excel')
w.writerows(LL)

C:\test.csv when opened looks like this:

1,2

3,4

1,2

3,4
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:43:39+00:00Added an answer on May 18, 2026 at 8:43 am

    Appending is irrelevant to the problem; notice that the first two rows (those from the original file) are also double-spaced.

    The real problem is that you have opened your file in text mode.

    CSV is a binary format, believe it or not. The csv module is writing the misleadingly-named “lineterminator (should be “rowseparator”) as \r\n as expected but then the Windows C runtime kicks in and replaces the \n with \r\n so that you have \r\r\n between rows. When you “open” the csv file with Excel it becomes confused

    Always open your CSV files in binary mode (‘rb’, ‘wb’, ‘ab’), whether you are operating on Windows or not. That way, you will get the expected rowseparator (CR LF) even on *x boxes, your code will be portable, and any linefeeds embedded in your data won’t be changed into something else (on writing) or cause dramas (on input, provided of course they’re quoted properly).

    Other problems:

    (1) Don’t put your data in your root directory (C:\). Windows inherited a hierarchical file system from MS-DOS in the 1980s. Use it.

    (2) If you must embed hard-wired filenames in your code, use raw strings r"c:\test.csv" … if you had "c:\test.csv" the ‘\t’ would be interpreted as a TAB character; similar problems with \r and \n

    (3) The examples in the Python manual are aligned more towards brevity than robust code.

    Don’t do this:

    w = csv.writer(open('foo.csv', 'wb'))
    

    Do this:

    f = open('foo.csv', 'wb')
    w = csv.writer(f)
    

    Then when you are finished, you have f available so that you can do f.close() to ensure that your file contents are flushed to disk. Even better: read up on the new with statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python, how would I go about reading in (be from a string, file
Using Python's Imaging Library I want to create a PNG file. I would like
Using Python I want to be able to draw text at different angles using
Using python 2.4 and the built-in ZipFile library, I cannot read very large zip
Using Python 2.6, is there a way to check if all the items of
When using Python's super() to do method chaining, you have to explicitly specify your
I want to start using Python for small projects but the fact that a
I'm using python and CherryPy to create a simple internal website that about 2
I am using python 2.6 on XP. I have just installed py2exe, and I
I'm using python and I need to map locations like Bloomington, IN to GPS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.