python 2: open and parse text file with mixed newline characters

Viewed 116

Using Python 2.7 (Anaconda Distribution): I need to parse a text file randomly terminating lines on either "\n" or "\r\n". When I use the open function with the option "rb" the script successfully interprets "\n" as a line terminator, but not "\r\n". When I use open with option "rU" (supposedly offers universal newlines support), the script breaks lines at "\r\n" but not at "\n". How can I open the file recognizing either of these different line terminators?

# recognizes "\n" but not "\r\n"
with open(infile, 'rb') as f:
    reader = csv.reader(f, delimiter='|')

# recognizes "\r\n" but not "\n"
with open(infile, 'rU') as f:
    reader = csv.reader(f, delimiter='|')
1 Answers

Is it an option to simply sanitize your input files before feeding them to the python script? dos2unix will do convert your mixed-case \r\n and \n endings to just \n ones for you.

Related