Python, read CRLF text file as is, with CRLF
问题 with open(fn, 'rt') as f: lines = f.readlines() This reads CR LF text file (WinXP, Py 2.6) with LF line ends. So lines contain '\n' ends. How to get lines as is: for CRLF file get lines with '\n\r' ends for LF file get lines with '\n' ends 回答1: Instead of the built-in open() function, use io.open(). This gives you more control over how newlines are handled with the newline argument: import io with io.open(fn, 'rt', newline='') as f: lines = f.readlines() Setting newline to the empty string,