Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3917

detecting newline character

Date 2011-04-23 20:09 +0200
From Daniel Geržo <danger@rulez.sk>
Subject detecting newline character
Newsgroups comp.lang.python
Message-ID <mailman.779.1303582172.9059.python-list@python.org> (permalink)

Show all headers | View raw


Hello guys,

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(codecs.open(self.path, "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 codecs.open() 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 codecs.open() with encoding='ascii' argument, the fobj.newlines is 
None and I can't figure out why that is the case. Reading the PEP at 
http://www.python.org/dev/peps/pep-0278/ 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 
http://danger.rulez.sk/subrip_ascii.srt

Thanks.

-- 
S pozdravom / Best regards
   Daniel Gerzo

Back to comp.lang.python | Previous | NextNext in thread | Find similar


Thread

detecting newline character Daniel Geržo <danger@rulez.sk> - 2011-04-23 20:09 +0200
  Re: detecting newline character Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-04-23 21:18 +0200
    Re: detecting newline character Daniel Geržo <danger@rulez.sk> - 2011-04-24 09:43 +0200
      [SOLVED] detecting newline character Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-04-24 15:35 +0200

csiph-web