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


Groups > comp.lang.python > #6280

Re: Python 3.2 bug? Reading the last line of a file

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!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
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; 'subject:bug': 0.04; '3.2': 0.05; 'subject:Python': 0.06; '>>>>': 0.09; 'from:addr:python': 0.09; 'str': 0.09; 'api': 0.11; 'binary': 0.14; 'subject:file': 0.14; 'wrote:': 0.14; '3.2,': 0.16; 'cleanly': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'literals': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'traceback': 0.16; 'tries': 0.16; '(most': 0.16; 'bytes': 0.19; 'issue.': 0.19; 'header:In-Reply-To:1': 0.21; 'last):': 0.23; 'vs.': 0.23; "doesn't": 0.25; 'received:84': 0.25; 'demonstrate': 0.26; 'string': 0.26; 'thanks': 0.28; 'mode': 0.29; 'unable': 0.30; 'if,': 0.30; 'typeerror:': 0.30; 'skip:b 30': 0.31; 'seem': 0.32; "skip:' 10": 0.32; 'to:addr:python-list': 0.33; 'list': 0.33; 'file': 0.34; 'header:User-Agent:1': 0.35; '"",': 0.35; 'reply-to:addr:python.org': 0.35; 'using': 0.35; 'issue': 0.37; 'instead.': 0.37; 'but': 0.38; 'docs': 0.38; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'under': 0.40; 'read,': 0.40; 'below.': 0.65; 'subject:? ': 0.67; 'email addr:hotmail.com': 0.72; 'header:Reply-To:1': 0.72; 'reply-to:no real name:2**0': 0.72; 'subject:line': 0.73; 'succeed': 0.73; '2.7.1': 0.84; 'respectively': 0.84
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result AlwJADmW3U3Unw4S/2dsb2JhbACYADGOBHjGRIYcBJRmhCGGMA
Date Thu, 26 May 2011 00:56:52 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10
MIME-Version 1.0
To python-list@python.org
Subject Re: Python 3.2 bug? Reading the last line of a file
References <3d81e2a0-6c86-4f12-a1c4-ce4c736172b6@y31g2000vbp.googlegroups.com> <4DDD5FD2.8040607@mrabarnett.plus.com> <BANLkTik1NyMO8vEfb-+oO_7jLD9B=+ZMRA@mail.gmail.com> <mailman.2096.1306360361.9059.python-list@python.org> <55262a36-ca53-48dd-b563-1847f9442bae@dn9g2000vbb.googlegroups.com>
In-Reply-To <55262a36-ca53-48dd-b563-1847f9442bae@dn9g2000vbb.googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
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.2100.1306367807.9059.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 82.94.164.166
X-Trace 1306367808 news.xs4all.nl 49038 [::ffff:82.94.164.166]:36628
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6280

Show key headers only | View raw


On 26/05/2011 00:25, tkpmep@hotmail.com wrote:
> Thanks for the guidance - it was indeed an issue with reading in
> binary vs. text., and I do now succeed in reading the last line,
> except that I now seem unable to split it, as I demonstrate below.
> Here's what I get when I read the last line in text mode using 2.7.1
> and in binary mode using 3.2 respectively under IDLE:
>
> 2.7.1
> Name	31/12/2009	0	0	0
>
> 3.2
> b'Name\t31/12/2009\t0\t0\t0\r\n'
>
> if, under 2.7.1 I read the file in text mode and write
>>>> x = lastLine(fn)
> I can then cleanly split the line to get its contents
>>>> x.split('\t')
> ['Name', '31/12/2009', '0', '0', '0\n']
>
> but under 3.2, with its binary read, I get
>>>> x.split('\t')
> Traceback (most recent call last):
>    File "<pyshell#26>", line 1, in<module>
>      x.split('\t')
> TypeError: Type str doesn't support the buffer API
>
> If I remove the '\t', the split now works and I get a list of bytes
> literals
>>>> x.split()
> [b'Name', b'31/12/2009', b'0', b'0', b'0']
>
> Looking through the docs did not clarify my understanding of the
> issue. Why can I not split on '\t' when reading in binary mode?
>
x.split('\t') tries to split on '\t', a string (str), but x is a
bytestring (bytes).

Do x.split(b'\t') instead.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python 3.2 bug? Reading the last line of a file "tkpmep@hotmail.com" <tkpmep@hotmail.com> - 2011-05-25 12:33 -0700
  Re: Python 3.2 bug? Reading the last line of a file MRAB <python@mrabarnett.plus.com> - 2011-05-25 21:00 +0100
  Re: Python 3.2 bug? Reading the last line of a file Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-25 14:54 -0600
  Re: Python 3.2 bug? Reading the last line of a file MRAB <python@mrabarnett.plus.com> - 2011-05-25 22:52 +0100
    Re: Python 3.2 bug? Reading the last line of a file "tkpmep@hotmail.com" <tkpmep@hotmail.com> - 2011-05-25 16:25 -0700
      Re: Python 3.2 bug? Reading the last line of a file Ethan Furman <ethan@stoneleaf.us> - 2011-05-25 16:58 -0700
      Re: Python 3.2 bug? Reading the last line of a file MRAB <python@mrabarnett.plus.com> - 2011-05-26 00:56 +0100
      Re: Python 3.2 bug? Reading the last line of a file Ethan Furman <ethan@stoneleaf.us> - 2011-05-25 17:32 -0700
      Re: Python 3.2 bug? Reading the last line of a file Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2011-05-26 08:09 +0300
        Re: Python 3.2 bug? Reading the last line of a file "tkpmep@hotmail.com" <tkpmep@hotmail.com> - 2011-05-27 12:21 -0700
  Re: Python 3.2 bug? Reading the last line of a file Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-25 19:06 -0600

csiph-web