detecting newline character

Thomas 'PointedEars' Lahn PointedEars at
Sat Apr 23 15:18:53 EDT 2011

Daniel Geržo wrote:

> I need to detect the newline characters used in the file I am reading.
> For this purpose I am using the following code:
> def _read_lines(self):
>      with contextlib.closing(, "rU")) as fobj:
>          fobj.readlines()
>          if isinstance(fobj.newlines, tuple):
>              self.newline = fobj.newlines[0]
>          else:
>              self.newline = fobj.newlines
> This works fine, if I call without encoding argument; I am
> testing with an ASCII enghlish text file, and in such case the
> fobj.newlines is correctly detected being as '\r\n'. However, when I
> call with encoding='ascii' argument, the fobj.newlines is
> None and I can't figure out why that is the case. Reading the PEP at
> I don't see any reason why
> would I end up with newlines being None after I call readlines().
> Anyone has an idea? You can fetch the file I am testing with from

I see nothing suspicious in your .srt *after* downloading it.  file -i 
confirms that it only contains US-ASCII characters (but see below).

The only reason I can think of for this not working ATM comes from the 
documentation, where it says that 'U' requires Python to be built with 
universal newline support; that it is *usually* so, but might not be so in 
your case (but then the question remains: How could it be not None without 
`encoding' argument?)


WFM with and without `encoding' argument in python-2.7.1-8 (CPython), Debian 
GNU/Linux 6.0.1, Linux (custom) SMP i686.

Which Python implementation and version are you using on which system?

On which system has the "ASCII" file been created and how?  Note that both 
uploading the file with FTP in ASCII mode and downloading over HTTP might 
have removed the problem Python has with it.


More information about the Python-list mailing list