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

  • Home
  • SEARCH
  • 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 9148107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:11:04+00:00 2026-06-17T11:11:04+00:00

I am trying to process a csv file in python that has ^M character

  • 0

I am trying to process a csv file in python that has ^M character in the middle of each row/line which is a newline. I cant open the file in any mode other than ‘rU’.

If I do open the file in the ‘rU’ mode, it reads in the newline and splits the file (creating a newline) and gives me twice the number of rows.

I want to remove the newline altogether. How?

  • 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-06-17T11:11:05+00:00Added an answer on June 17, 2026 at 11:11 am

    Note that, as the docs say:

    csvfile can be any object which supports the iterator protocol and returns a string each time its next() method is called — file objects and list objects are both suitable.

    So, you can always stick a filter on the file before handing it to your reader or DictReader. Instead of this:

    with open('myfile.csv', 'rU') as myfile:
        for row in csv.reader(myfile):
    

    Do this:

    with open('myfile.csv', 'rU') as myfile:
        filtered = (line.replace('\r', '') for line in myfile)
        for row in csv.reader(filtered):
    

    That '\r' is the Python (and C) way of spelling ^M. So, this just strips all ^M characters out, no matter where they appear, by replacing each one with an empty string.


    I guess I want to modify the file permanently as opposed to filtering it.

    First, if you want to modify the file before running your Python script on it, why not do that from outside of Python? sed, tr, many text editors, etc. can all do this for you. Here’s a GNU sed example:

    gsed -i'' 's/\r//g' myfile.csv
    

    But if you want to do it in Python, it’s not that much more verbose, and you might find it more readable, so:

    First, you can’t really modify a file in-place if you want to insert or delete from the middle. The usual solution is to write a new file, and either move the new file over the old one (Unix only) or delete the old one (cross-platform).

    The cross-platform version:

    os.rename('myfile.csv', 'myfile.csv.bak')
    with open('myfile.csv.bak', 'rU') as infile, open('myfile.csv', 'wU') as outfile:
        for line in infile:
            outfile.write(line.replace('\r'))
    os.remove('myfile.csv.bak')
    

    The less-clunky, but Unix-only, version:

    temp = tempfile.NamedTemporaryFile(delete=False)
    with open('myfile.csv', 'rU') as myfile, closing(temp):
        for line in myfile:
            temp.write(line.replace('\r'))
    os.rename(tempfile.name, 'myfile.csv')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to process a CSV file in which some of the fields
I am trying to write into csv file using Kent.Boogaart.KBCsv, cant figure out what
I am trying to read a bzip2-compressed CSV file in Python 3.2. For an
I am trying to process a csv file using awk. I have five rows
I have a CSV file I need to process which is a bit of
I am trying to parse a csv file and in the process have come
This is what I am trying to achieve: I have a CSV file which
I am currently trying to process a csv file in PHP using preg_match(). An
I currently have a CSV file that I parse and am trying to insert
I'm trying to process an uploaded CSV file into an array in PHP. I

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.