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


Groups > comp.lang.python > #53707

Re: split lines from stdin into a list of unicode strings

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <kurt.alfred.mueller@gmail.com>
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; 'else:': 0.03; 'encoding': 0.05; 'much!': 0.05; 'none:': 0.07; 'wednesday,': 0.07; 'ascii': 0.09; 'false.': 0.09; 'lines.': 0.09; 'subject:into': 0.09; 'tackle': 0.09; 'template.': 0.09; 'try:': 0.09; 'undefined': 0.09; 'url:github': 0.09; 'runs': 0.10; 'def': 0.12; 'kurt': 0.12; 'template': 0.14; 'binary,': 0.16; 'character.': 0.16; 'charge,': 0.16; 'detected': 0.16; 'skipping': 0.16; 'subject:unicode': 0.16; 'url:linux': 0.16; 'vary.': 0.16; 'size,': 0.16; 'wrote:': 0.18; 'library': 0.18; 'file,': 0.19; 'split': 0.19; '>>>': 0.22; 'input': 0.22; 'tests': 0.22; 'header:User-Agent:1': 0.23; 'fine': 0.24; 'non': 0.24; 'script': 0.25; 'pass': 0.26; 'header:In-Reply- To:1': 0.27; 'errors': 0.30; 'subject:list': 0.30; 'lines': 0.31; '>>>>': 0.31; 'text': 0.33; 'skip:d 20': 0.34; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'except': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'yield': 0.36; 'list': 0.37; 'message- id:@gmail.com': 0.38; 'thank': 0.38; 'tasks': 0.38; 'to:addr :python-list': 0.38; 'files': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'dave': 0.60; 'august': 0.61; 'skip:a 30': 0.61; 'url:3': 0.61; 'took': 0.61; 'email addr:gmail.com': 0.63; 'real': 0.63; 'different': 0.65; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'reply-to:addr:gmail.com': 0.80; 'issues:': 0.84; 'angel': 0.91; 'luck': 0.93; 'tough': 0.93; '2013': 0.98
X-Virus-Scanned amavisd-new at aerodynamics.ch
Date Thu, 05 Sep 2013 15:25:50 +0200
From Kurt Mueller <kurt.alfred.mueller@gmail.com>
Organization Rothenburg
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8
MIME-Version 1.0
To python-list@python.org
Subject Re: split lines from stdin into a list of unicode strings
References <521DB58E.5000102@gmail.com> <mailman.302.1377688438.19984.python-list@python.org> <b67f4179-53c6-4bd4-b9ea-8852c9048be0@googlegroups.com> <kvn399$a3r$1@ger.gmane.org> <522835EC.1010407@gmail.com> <l09fki$rfa$1@ger.gmane.org>
In-Reply-To <l09fki$rfa$1@ger.gmane.org>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To kurt.alfred.mueller@gmail.com
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.95.1378387574.5461.python-list@python.org> (permalink)
Lines 82
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1378387575 news.xs4all.nl 15898 [2001:888:2000:d::a6]:53471
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:53707

Show key headers only | View raw


Am 05.09.2013 10:33, schrieb Peter Otten:
> Kurt Mueller wrote:
>> Am 29.08.2013 11:12, schrieb Peter Otten:
>>> kurt.alfred.mueller@gmail.com wrote:
>>>> On Wednesday, August 28, 2013 1:13:36 PM UTC+2, Dave Angel wrote:
>>>>> On 28/8/2013 04:32, Kurt Mueller wrote:
>>>>>> For some text manipulation tasks I need a template to split lines
>>>>>> from stdin into a list of strings the way shlex.split() does it.
>>>>>> The encoding of the input can vary.
>> I took your script as a template.
>> But I used the libmagic library (pyhton-magic) instead of chardet.
>> See http://linux.die.net/man/3/libmagic
>> and https://github.com/ahupp/python-magic
>> ( I made tests with files of different size, up to 1.2 [GB] )
>> I had following issues:
>> - I a real file, the encoding was detected as 'ascii' for
>> detect_lines=1000.
>>   In line 1002 there was an umlaut character. So then the
>>   line.decode(encoding) failed. I think to add the errors parameter,
>>   line.decode(encoding, errors='replace')
> 
> Tough luck ;) You could try and tackle the problem by skipping leading 
> ascii-only lines. Untested:
> 
> def detect_encoding(instream, encoding, detect_lines, skip_ascii=True):
>     if encoding is None:
>         encoding = instream.encoding
>         if encoding is None:
>             if skip_ascii:
>                 try:
>                     for line in instream:
>                         yield line.decode("ascii")
>                 except UnicodeDecodeError:
>                     pass
>                 else:
>                     return
>             head = [line]
>             head.extend(islice(instream, detect_lines-1))
>             encoding =  chardet.detect("".join(head))["encoding"]
>             instream = chain(head, instream)
>     for line in instream:
>         yield line.decode(encoding)

I find this solution as a generator very nice.
With just some small modifications it runs fine for now.
( line is undefined if skip_ascii is False. )

For ascii only files chardet or libmagic will not be bothered.
And the detect_lines comes not in charge, until there are
some non ascii characters.

------------------------------------------------------------------------------
def decode_stream_lines( inpt_strm, enco_type, numb_inpt, skip_asci=True, ):
    if enco_type is None:
        enco_type = inpt_strm.encoding
        if enco_type is None:
            line_head = []
            if skip_asci:
                try:
                    for line in inpt_strm:
                        yield line.decode( 'ascii' )
                except UnicodeDecodeError:
                    line_head = [ line ] # last line was not ascii
                else:
                    return # all lines were ascii
            line_head.extend( islice( inpt_strm, numb_inpt - 1 ) )
            magc_enco = magic.open( magic.MAGIC_MIME_ENCODING )
            magc_enco.load()
            enco_type = magc_enco.buffer( "".join( line_head ) )
            magc_enco.close()
            print( I_AM + '-ERROR: enco_type=' + repr( enco_type ), file=sys.stderr, )
            if  enco_type.rfind( 'binary' ) >= 0: # binary, application/mswordbinary, application/vnd.ms-excelbinary and the like
                return
            inpt_strm = chain( line_head, inpt_strm )
    for line in inpt_strm:
        yield line.decode( enco_type, errors='replace' )
------------------------------------------------------------------------------


Thank you very much!
-- 
Kurt Mueller

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


Thread

Re: split lines from stdin into a list of unicode strings Dave Angel <davea@davea.name> - 2013-08-28 11:13 +0000
  Re: split lines from stdin into a list of unicode strings kurt.alfred.mueller@gmail.com - 2013-08-28 05:39 -0700
    Re: split lines from stdin into a list of unicode strings Peter Otten <__peter__@web.de> - 2013-08-29 11:12 +0200
    Re: split lines from stdin into a list of unicode strings Kurt Mueller <kurt.alfred.mueller@gmail.com> - 2013-08-29 13:31 +0200
    Re: split lines from stdin into a list of unicode strings Peter Otten <__peter__@web.de> - 2013-08-29 15:15 +0200
    Re: split lines from stdin into a list of unicode strings Kurt Mueller <kurt.alfred.mueller@gmail.com> - 2013-09-05 09:42 +0200
    Re: split lines from stdin into a list of unicode strings Peter Otten <__peter__@web.de> - 2013-09-05 10:33 +0200
    Re: split lines from stdin into a list of unicode strings Kurt Mueller <kurt.alfred.mueller@gmail.com> - 2013-09-05 15:25 +0200

csiph-web