I am writing a Python file that needs to read in several files of different types. I am reading the files in line by line with the traditional for line in f after using f = open("file.txt", "r").
This doesn’t seem to be working for all files. My guess is some files end with different encodings (such as \r\n versus just \r). I can read the whole file in and do a string split on \r, but that is hugely costly and I’d rather not. Is there a way to make the readline method of Python recognize both end-of-line variations?
Use the universal newline support — see http://docs.python.org/library/functions.html#open