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 943827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:29:48+00:00 2026-05-15T22:29:48+00:00

import csv with open(‘thefile.csv’, ‘rb’) as f: data = list(csv.reader(f)) import collections counter =

  • 0
import csv

with open('thefile.csv', 'rb') as f:
  data = list(csv.reader(f))
  import collections
  counter = collections.defaultdict(int)

  for row in data:
        counter[row[10]] += 1


with open('/pythonwork/thefile_subset11.csv', 'w') as outfile:
    writer = csv.writer(outfile)
    for row in data:
        if counter[row[10]] >= 504:
           writer.writerow(row)

This code reads thefile.csv, makes changes, and writes results to thefile_subset1.

However, when I open the resulting csv in Microsoft Excel, there is an extra blank line after each record!

Is there a way to make it not put an extra blank line?

  • 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-15T22:29:49+00:00Added an answer on May 15, 2026 at 10:29 pm

    The csv.writer module directly controls line endings and writes \r\n into the file directly. In Python 3 the file must be opened in untranslated text mode with the parameters 'w', newline='' (empty string) or it will write \r\r\n on Windows, where the default text mode will translate each \n into \r\n.

    #!python3
    with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile:
        writer = csv.writer(outfile)
    

    If using the Path module:

    from pathlib import Path
    import csv
    
    with Path('/pythonwork/thefile_subset11.csv').open('w', newline='') as outfile:
        writer = csv.writer(outfile)
    

    If using the StringIO module to build an in-memory result, the result string will contain the translated line terminator:

    from io import StringIO
    import csv
    
    s = StringIO()
    writer = csv.writer(s)
    writer.writerow([1,2,3])
    print(repr(s.getvalue()))  # '1,2,3\r\n'   (Windows result)
    

    If writing that string to a file later, remember to use newline='':

    # built-in open()
    with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as f:
        f.write(s.getvalue())
    
    # Path's open()
    with Path('/pythonwork/thefile_subset11.csv').open('w', newline='') as f:
        f.write(s.getvalue())
    
    # Path's write_text() added the newline parameter to Python 3.10.
    Path('/pythonwork/thefile_subset11.csv').write_text(s.getvalue(), newline='')
    

    In Python 2, use binary mode to open outfile with mode 'wb' instead of 'w' to prevent Windows newline translation. Python 2 also has problems with Unicode and requires other workarounds to write non-ASCII text. See the Python 2 link below and the UnicodeReader and UnicodeWriter examples at the end of the page if you have to deal with writing Unicode strings to CSVs on Python 2, or look into the 3rd party unicodecsv module:

    #!python2
    with open('/pythonwork/thefile_subset11.csv', 'wb') as outfile:
        writer = csv.writer(outfile)
    

    Documentation Links

    • https://docs.python.org/3/library/csv.html#csv.writer
    • https://docs.python.org/2/library/csv.html#csv.writer
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import csv with open('thefile.csv', 'rb') as f: data = list(csv.reader(f)) import collections counter =
import csv with open('test.csv', 'rb') as f: data = list(csv.reader(f)) import collections counter =
import csv sample row = 10/6/2010,73.42,74.43,72.9,74.15,2993500 filename_in = 'c:/python27/scripts/fiverows.csv' reader = csv.reader(open(filename_in, rb), dialect=excel,
i am opening a csv file like this: import csv reader = csv.reader(open(book1.csv, rb))
In a basic I had the next process. import csv reader = csv.reader(open('huge_file.csv', 'rb'))
import csv from geopy import geocoders g = geocoders.Google() spamReader = csv.reader(open('locations.csv', 'rb'), delimiter='\t',
import csv datafile = csv.reader(open('datafile.csv','rb'), delimiter=,, quotechar='|') date, data1, data2, data3 = [], [],
import csv from geopy import geocoders import time g = geocoders.GeocoderDotUS() spamReader = csv.reader(open('locations.csv',
import csv from geopy import geocoders import time g = geocoders.GeocoderDotUS() spamReader = csv.reader(open('locations.csv',
I am trying to import a csv file to insert data into an existing

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.