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


Groups > comp.lang.python > #6282

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

Date 2011-05-25 17:32 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Python 3.2 bug? Reading the last line of a file
References (1 earlier) <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> <4DDD9744.1010002@mrabarnett.plus.com>
Newsgroups comp.lang.python
Message-ID <mailman.2101.1306369224.9059.python-list@python.org> (permalink)

Show all headers | View raw


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

<nitpick>
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'
</nitpick>

~Ethan~

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