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


Groups > comp.lang.python > #94724

Re: What happens when python seeks a text file

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:text': 0.04; 'encoded': 0.05; '21,': 0.07; 'bytes.': 0.07; 'character,': 0.07; 'subject:file': 0.07; '[0,': 0.09; 'indicates': 0.09; 'exception': 0.13; 'subject:python': 0.14; 'encoding': 0.15; '32,': 0.16; '42,': 0.16; '>that': 0.16; 'beginning.': 0.16; 'boundary,': 0.16; 'codec': 0.16; 'decode': 0.16; 'decoding': 0.16; 'encodings': 0.16; 'enough.': 0.16; 'f.tell()': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'sequence,': 0.16; 'set,': 0.16; 'simpson': 0.16; 'skip:> 20': 0.16; 'subject:when': 0.16; 'wrote:': 0.16; 'byte': 0.18; 'text,': 0.22; 'cheers,': 0.22; 'bit': 0.23; 'tried': 0.24; '(most': 0.24; 'plain': 0.24; 'somewhere': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'skip:" 20': 0.26; 'error': 0.27; 'sequence': 0.27; 'arithmetic': 0.29; 'character': 0.29; 'there.': 0.30; 'e.g.': 0.30; "can't": 0.32; 'returned': 0.32; 'problem': 0.33; 'url:python': 0.33; 'raised': 0.33; 'received:com.au': 0.33; 'safely': 0.33; 'shift': 0.33; 'traceback': 0.33; 'file': 0.34; 'text': 0.35; 'skip:. 20': 0.35; 'knowledge': 0.35; 'level': 0.35; 'but': 0.36; 'should': 0.36; 'needed': 0.36; 'there': 0.36; 'url:org': 0.36; 'mode': 0.36; 'url:library': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'available.': 0.37; 'things': 0.38; 'several': 0.38; 'hi,': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'url:3': 0.60; 'skip:u 10': 0.61; 'spot': 0.63; 'places': 0.64; 'cameron': 0.66; 'reverse': 0.66; 'legal': 0.66; 'subject': 0.70; '8bit%:100': 0.70; 'obtained': 0.76; 'actually,': 0.84; 'url:tutorial': 0.91; 'employ': 0.95
Date Wed, 29 Jul 2015 17:58:55 +1000
From Cameron Simpson <cs@zip.com.au>
To python-list@python.org
Subject Re: What happens when python seeks a text file
MIME-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Disposition inline
Content-Transfer-Encoding 8bit
In-Reply-To <876153v7c3.fsf@handshake.de>
User-Agent Mutt/1.5.23 (2014-03-12)
References <876153v7c3.fsf@handshake.de>
X-Optus-CM-Score 0
X-Optus-CM-Analysis v=2.1 cv=eZjABOwH c=1 sm=1 tr=0 a=7VRakydLWw4CnqwyGz1TSg==:117 a=7VRakydLWw4CnqwyGz1TSg==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=vrnE16BAAAAA:8 a=yEdEr6MRgwAA:10 a=IkcTkHD0fZMA:10 a=zOBTXjUuO1YA:10 a=gnbuhwpKAAAA:8 a=8AHkEIZyAAAA:8 a=J3Qk1URDkS6cRuurBK0A:9 a=QEXdDO2ut3YA:10 a=j1U0AGED2WkA:10 a=XDVQQWv3cIUA:10
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1060.1438158394.3674.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1438158394 news.xs4all.nl 2834 [2001:888:2000:d::a6]:50208
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:94724

Show key headers only | View raw


On 29Jul2015 07:52, dieter <dieter@handshake.de> wrote:
>"=?GBK?B?wO68zsX0?=" <lijpbasin@126.com> writes:
>> Hi, I tried using seek to reverse a text file after reading about the
>> subject in the documentation:
>> https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects
>> https://docs.python.org/3/library/io.html#io.TextIOBase.seek
>> ...
>> However, an exception is raised if a file with the same content encoded in
>> GBK is provided:
>>     $ ./reverse_text_by_seek3.py Moon-gbk.txt
>>     [0, 7, 8, 19, 21, 32, 42, 53, 64]
>>     µÍͷ˼¹ÊÏç
>>     ¾ÙÍ·ÍûÃ÷ÔÂ
>>     Traceback (most recent call last):
>>       File "./reverse_text_by_seek3.py", line 21, in <module>
>>         print(f.readline(), end="")
>>     UnicodeDecodeError: 'gbk' codec can't decode byte 0xaa in position 8: illegal multibyte sequence
>
>The "seek" works on byte level while decoding works on character level
>where some characters can be composed of several bytes.
>
>The error you observe indicates that you have "seeked" somewhere
>inside a character, not at a legal character beginning.
>
>That you get an error for "gbk" and not for "utf-8" is a bit of
>an "accident". The same problem can happen for "utf-8" but the probability
>might by sligtly inferior.
>
>Seek only to byte position for which you know that they are also
>character beginnings -- e.g. line beginnings.

You may also keep in mind that while you can't do arithmetic on these things 
without knowning the length of the _encoded_ text, what you can do is note the 
value returned by f.tell() whenever you like. If you are reading a text file 
(== an encoding of the text in a specific character set, be it GBK or UTF8) 
then after any read you will be on a character boundary, and can return there.

Actually, on reflection, there may be some character encodings where this is 
not true; I think some encodings of Japanese employ some kind of mode shift 
sequence, so you might need knowledge of those - a plain seek() might not be 
enough. But for any encoding where the character encoded at a spot is 
everything needed then a seek() to any position obtained by tell() should be 
reliable.

In short: line beginnings are not the only places where you can safely seek.  
Though they may be conveniently available.

Cheers,
Cameron Simpson <cs@zip.com.au>

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


Thread

Re: What happens when python seeks a text file Cameron Simpson <cs@zip.com.au> - 2015-07-29 17:58 +1000

csiph-web