Python convert Windows CRLF to Mac CR ( line feeds)

Hi there

Does anyone know how to write a Python script that will convert all occurrences of a Windows carriage return/line feed in a document to Mac carriage returns?

Thanks in advance and Merry Christmas!

Charlie
LVL 1
butterhookAsked:
Who is Participating?
 
SuperdaveConnect With a Mentor Commented:
If by "document" you mean "text file", this should do it.

Merry Christmas to you too!

import sys
line=sys.stdin.readline()
while line:
	sys.stdout.write(line.strip('\n\r')+'\r')
	line=sys.stdin.readline()

Open in new window

0
 
James MurrellProduct SpecialistCommented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for Superdave's comment http:/Q_27507427.html#37331956

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
butterhookAuthor Commented:
Hello
I'd like to accept the answer, but can't seem to do so... any ideas?
Charlie
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
peprCommented:
However, if you convert the text file from Windows on your Mac, it is probably better to open it as a text file using the universal line ending convertsion and then to write it back to another text file open for writing.

Another approach is to read whole content in binary mode, delete all \n characters, and write it back.
0
 
peprCommented:
You can try the following scripts.  The first is to be run on the Mac target system to convert whatever line endings to the Mac ones.  (It can be run also on Windows to convert the line endings to the Windows ones, or on Unix to convert them to Unix ones.

whatever2this.py
import shutil

def convertLineEndings(fname):
    fnamebak = fname + '.bak'
    shutil.copy2(fname, fnamebak)  # just to be safe a bit
    
    # Open for reading in text mode with universal line endings.  Read 
    # the content of the whole file.  This is fine if the file is not
    # extremely big.  Then close the file.
    f = open(fname, 'Ur')  
    content = f.read()
    f.close()
    
    # Open the same file again, now for writing.  The line endings were
    # already converted during reading the content.  Just write it back.
    f = open(fname, 'w')
    f.write(content)
    f.close()
    
    
if __name__ == '__main__':
    convertLineEndings('a.txt')   

Open in new window


The following script can be run on whatever OS to convert the file line endings to the Mac ones (i.e. the CR only).

whatever2mac.py
def convertLineEndingsForMac(fname):
    fnameresult = 'forMac' + fname
    
    # Open for reading in binary mode.  Read the content of the whole file.
    f = open(fname, 'rb')  
    content = f.read()
    f.close()
    
    # Open the target file for writing in binary. Find, if there are some CR's.
    # If yes, remove all the LF's from the content.  Otherwise, replace the
    # LF's by CR's.  (Otherwise, you would loose line endings that were
    # formed only by LF's.) Write the modified content to the file.
    #
    # (After testing, modify the code so that it writes it back to the same file.)
    f = open(fnameresult, 'wb')
    if content.find('\r') >= 0:
        f.write(content.replace('\n', ''))
    else:
        f.write(content.replace('\n', '\r'))
    f.close()
    
    
if __name__ == '__main__':
    convertLineEndingsForMac('cr.txt')   
    convertLineEndingsForMac('crlf.txt')   
    convertLineEndingsForMac('lf.txt')   

Open in new window


toMac.zip
0
 
butterhookAuthor Commented:
Thanks pepr for your answer, didn't see it when I accepted the answer!!
0
 
peprCommented:
No problem ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.