Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'mrab': 0.05; 'subject:Python': 0.06; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; 'str': 0.09; '~ethan~': 0.09; 'api': 0.11; 'binary': 0.14; 'subject:file': 0.14; 'wrote:': 0.14; '3.2,': 0.16; 'cleanly': 0.16; 'integers.': 0.16; 'literals': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'traceback': 0.16; 'tries': 0.16; '(most': 0.16; 'bytes': 0.19; 'object,': 0.19; 'issue.': 0.19; 'header:In-Reply-To:1': 0.21; 'integer': 0.23; 'last):': 0.23; 'vs.': 0.23; "doesn't": 0.25; 'demonstrate': 0.26; 'string': 0.26; 'object': 0.26; 'thanks': 0.28; 'mode': 0.29; 'lists': 0.29; 'class': 0.29; 'unable': 0.30; 'if,': 0.30; 'typeerror:': 0.30; 'skip:b 30': 0.31; 'seem': 0.32; "skip:' 10": 0.32; 'does': 0.33; 'to:addr:python-list': 0.33; 'list': 0.33; 'rather': 0.34; 'file': 0.34; 'header:User-Agent:1': 0.35; '"",': 0.35; 'instances': 0.35; 'using': 0.35; 'issue': 0.37; 'element': 0.37; 'instead.': 0.37; 'but': 0.38; 'docs': 0.38; 'subject:: ': 0.38; 'called': 0.39; 'i.e.': 0.39; 'to:addr:python.org': 0.39; 'under': 0.40; 'read,': 0.40; 'really': 0.40; 'more': 0.60; 'below.': 0.65; 'received:websitewelcome.com': 0.67; 'subject:? ': 0.67; 'email addr:hotmail.com': 0.72; 'subject:line': 0.73; 'succeed': 0.73; 'received:69.56': 0.77; '2.7.1': 0.84; 'received:69.56.148': 0.84; 'respectively': 0.84; 'contrast': 0.91 Date: Wed, 25 May 2011 17:32:58 -0700 From: Ethan Furman User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) 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> <55262a36-ca53-48dd-b563-1847f9442bae@dn9g2000vbb.googlegroups.com> <4DDD9744.1010002@mrabarnett.plus.com> In-Reply-To: <4DDD9744.1010002@mrabarnett.plus.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:2184 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 2 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306369224 news.xs4all.nl 49047 [::ffff:82.94.164.166]:35415 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6282 MRAB wrote: > 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 "", line 1, in >> 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. Instances of the bytes class are more appropriately called 'bytes objects' rather than 'bytestrings' as they are really lists of integers. Accessing a single element of a bytes object does not return a bytes object, but rather the integer at that location; i.e. --> b'xyz'[1] 121 Contrast that with the str type where --> 'xyz'[1] 'y' ~Ethan~