Saltycrane logo

SaltyCrane Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

How to remove ^M characters from a file with Python

Use the following Python script to remove ^M (carriage return) characters from your file and replace them with newline characters only. To do this in Emacs, see my notes here.

remove_ctrl_m_chars.py:

import os
import sys
import tempfile


def main():
    filename = sys.argv[1]
    with tempfile.NamedTemporaryFile(delete=False) as fh:
        for line in open(filename):
            line = line.rstrip()
            fh.write(line + '\n')
        os.rename(filename, filename + '.bak')
        os.rename(fh.name, filename)


if __name__ == '__main__':
    main()

Run it

$ python remove_ctrl_m_chars.py myfile.txt 

Documentation

4 Comments — feed icon Comments feed for this post


#1 Harsh commented on 2011-11-23:

Why would you need this as a utility when you have dos2unix, whose entire purpose is for this?


#2 Eliot commented on 2011-11-23:

Harsh: Because I have a bad case of NIH and because I wasn't familiar with dos2unix. Thanks for the tip; I'll use dos2unix next time.


#3 Kaixi commented on 2011-12-04:

Wonderful little script :)


#4 Khalil commented on 2011-12-30:

or use vi editor's command

:%s/r//g

Post a comment

Required
Required, but not displayed
Optional

Format using Markdown. (No HTML.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab (and a blank line before and after)
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
Created with Django | Hosted by Linode