Convert \r text to \n so readlines() works as intended Convert \r text to \n so readlines() works as intended python python

Convert \r text to \n so readlines() works as intended


f = open('file.txt','rU')

This opens the file with Python's universal newline support and \r is treated as an end-of-line.


If it's a concern, open in binary format and convert with this code:

from __future__ import with_statementwith open(filename, "rb") as f:    s = f.read().replace('\r\n', '\n').replace('\r', '\n')    lines = s.split('\n')