Idiomatic portable way to strip line endings?
Skip Montanaro
skip at pobox.com
Sun Dec 16 13:50:45 EST 2001
> I've been trying to figure out the canonical way to strip the line
> endings from a text file.
I suspect the high frequency of
line = line[:-1]
simply means that most people haven't worried too much about their code
running across multiple platforms. I know for most of the code I've written
I've never worried too much about it. Still, how about:
line = re.sub(r'(\r\n|\r|\n)$', '', line)
? In a little filter script I use to convert to canonical line endings I
use
sys.stdout.write(re.sub(r'\r\n|\r', r'\n', sys.stdin.read()))
(This for a set of filters that processes web pages in various ways.)
--
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)
More information about the Python-list
mailing list