Detecting line endings

Fuzzyman fuzzyman at gmail.com
Mon Feb 6 09:35:14 EST 2006


Hello all,

I'm trying to detect line endings used in text files. I *might* be
decoding the files into unicode first (which may be encoded using
multi-byte encodings) - which is why I'm not letting Python handle the
line endings.

Is the following safe and sane :

text = open('test.txt', 'rb').read()
if encoding:
    text = text.decode(encoding)
ending = '\n' # default
if '\r\n' in text:
    text = text.replace('\r\n', '\n')
    ending = '\r\n'
elif '\n' in text:
    ending = '\n'
elif '\r' in text:
    text = text.replace('\r', '\n')
    ending = '\r'


My worry is that if '\n' *doesn't* signify a line break on the Mac,
then it may exist in the body of the text - and trigger ``ending =
'\n'`` prematurely ?

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml




More information about the Python-list mailing list