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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SuperdaveCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference ithrough the Colors of STEM program.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.