Dealing with Windows line-endings in Python

Posted by Adam Nelson on Stack Overflow See other posts from Stack Overflow or by Adam Nelson
Published on 2010-04-26T21:37:51Z Indexed on 2010/04/26 21:53 UTC
Read the original article Hit count: 221

Filed under:
|

I've got a 700MB XML file coming from a Windows provider.

As one might expect, the line endings are '\r\n' (or ^M in vi). What is the most efficient way to deal with this situation aside from getting the supplier to send over '\n' :-)

  1. Use os.linesep
  2. Use rstrip() (requiring opening the file ... which seems crazy)
  3. Using Universal newline support is not standard on my Mac Snow Leopard - so isn't an option.

I'm open to anything that requires Python 2.6+ but it needs to work on Snow Leopard and Ubuntu 9.10 with minimal external requirements. I don't mind a small performance penalty but I am looking for the standard best way to deal with this.

© Stack Overflow or respective owner

Related posts about python

Related posts about files