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


Groups > comp.lang.python > #3942

Re: detecting newline character

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <danger@rulez.sk>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'ascii': 0.07; 'below).': 0.07; 'fine,': 0.07; 'pep': 0.07; 'system?': 0.07; 'python': 0.07; 'argument,': 0.09; 'attribute': 0.09; 'encoding.': 0.09; 'fetch': 0.09; 'newline': 0.09; 'url:bitbucket': 0.09; 'url:dev': 0.09; 'url:peps': 0.09; 'linux': 0.11; 'def': 0.13; 'wrote:': 0.14; '*after*': 0.16; 'newlines': 0.16; 'tuple):': 0.16; 'uploading': 0.16; 'url:sk': 0.16; 'argument': 0.16; 'case.': 0.16; 'figure': 0.18; "wouldn't": 0.18; 'code': 0.22; 'header:In-Reply-To:1': 0.22; '(but': 0.22; 'fail': 0.22; 'file,': 0.22; 'debian': 0.23; "didn't": 0.25; 'calling': 0.25; 'version': 0.25; 'detect': 0.25; 'ftp': 0.25; 'installation': 0.25; 'says': 0.25; '(e.g.': 0.26; 'tests': 0.26; 'environment': 0.26; 'tried': 0.27; 'testing': 0.28; 'noticed': 0.29; 'problem': 0.29; 'daniel': 0.29; 'mode': 0.29; 'universal': 0.29; 'all.': 0.30; 'enabled,': 0.31; 'idea?': 0.31; 'url:src': 0.31; 'does': 0.31; 'it.': 0.31; 'however,': 0.31; "can't": 0.31; 'url:library': 0.31; 'anyone': 0.31; "skip:' 10": 0.32; 'to:addr :python-list': 0.32; 'source': 0.32; 'url:docs': 0.33; 'created': 0.33; 'using': 0.34; 'regular': 0.34; 'requires': 0.35; 'file': 0.35; 'characters': 0.35; 'correctly': 0.35; 'question': 0.35; 'header:User-Agent:1': 0.35; 'quite': 0.36; 'think': 0.36; 'none': 0.36; 'some': 0.37; 'case': 0.37; 'url:python': 0.37; 'either': 0.37; 'http': 0.38; 'identified': 0.38; 'but': 0.38; 'code:': 0.38; 'url:org': 0.38; 'so,': 0.38; 'used': 0.38; 'built': 0.39; 'end': 0.39; 'set': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'comes': 0.39; 'where': 0.39; 'how': 0.39; 'works': 0.40; 'would': 0.40; 'header:Received:5': 0.40; 'might': 0.40; 'best': 0.60; 'atm': 0.68; 'received:188': 0.75; '100%': 0.82; '2.7.1': 0.84; 'argument;': 0.84; 'received:188.40': 0.84; 'skip:~ 30': 0.84; 'subtitle': 0.84; 'suspicious': 0.84; 'url:functions': 0.84; 'confirms': 0.91; 'url:open': 0.93
X-Virus-Scanned amavisd-new at rulez.sk
Date Sun, 24 Apr 2011 09:43:58 +0200
From Daniel Geržo <danger@rulez.sk>
User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.17pre) Gecko/20110414 Lanikai/3.1.10pre
MIME-Version 1.0
To python-list@python.org
Subject Re: detecting newline character
References <mailman.779.1303582172.9059.python-list@python.org> <2887713.8pfo1IzOAr@PointedEars.de>
In-Reply-To <2887713.8pfo1IzOAr@PointedEars.de>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.789.1303631049.9059.python-list@python.org> (permalink)
Lines 73
NNTP-Posting-Host 82.94.164.166
X-Trace 1303631050 news.xs4all.nl 81479 [::ffff:82.94.164.166]:47001
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3942

Show key headers only | View raw


On 23.4.2011 21:18, Thomas 'PointedEars' Lahn wrote:
> 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(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
>
> I see nothing suspicious in your .srt *after* downloading it.  file -i
> confirms that it only contains US-ASCII characters (but see below).

That is indeed the case in my environment too.

danger@[danger-mbp ~/devel/pysublib/pysublib/test/files]> file -i 
subrip_ascii.srt
subrip_ascii.srt: regular file
danger@[danger-mbp ~/devel/pysublib/pysublib/test/files]> file 
subrip_ascii.srt
subrip_ascii.srt: ASCII English text, with CRLF line terminators


> 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?)

Yes, this is what does not make sense. If I didn't have the universal 
newline support enabled, I wouldn't have the newlines attribute at all.

> <http://docs.python.org/library/codecs.html?highlight=codecs.open#codecs.open>
> <http://docs.python.org/library/functions.html#open>
>
> WFM with and without `encoding' argument in python-2.7.1-8 (CPython), Debian
> GNU/Linux 6.0.1, Linux 2.6.35.5-pe (custom) SMP i686.
>
> Which Python implementation and version are you using on which system?

This is a standard python installation from MacPorts. System is OS X 
10.6.7. I have now tried both python 2.7.1 and python 2.6.6 from 
MacPorts and also 2.6.6 on FreeBSD. All fail for me when I set encoding.

> 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.

Unfortunately I am not 100% sure where I created the file, it was quite 
some time ago, but it was either WinXP, or OS X Leopard. The source code 
can be found at https://bitbucket.org/danger/pysublib/src - I noticed 
the subtitle file tests (e.g. test/test_subripfile.py) are failing for 
me and I have identified the problem with newlines being None after 
calling read().

-- 
S pozdravom / Best regards
   Daniel Gerzo

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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