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


Groups > comp.lang.python > #101097

Re: Newbie: Check first two non-whitespace characters

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Cameron Simpson <cs@zip.com.au>
Newsgroups comp.lang.python
Subject Re: Newbie: Check first two non-whitespace characters
Date Fri, 1 Jan 2016 17:30:43 +1100
Lines 35
Message-ID <mailman.134.1451629854.11925.python-list@python.org> (permalink)
References <56857609.9080603@mrabarnett.plus.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii; format=flowed
X-Trace news.uni-berlin.de bVdtmZElP7pk1n6Lw1rv7Q5o9Y+0SfSbFvG88kgUHk/w==
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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'ideally': 0.04; 'string.': 0.04; 'mrab': 0.05; 'subject:two': 0.07; 'cc:addr:python-list': 0.09; 'subject:characters': 0.09; '1))': 0.16; '>on': 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; 'quoted': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 's[1:]': 0.16; 'simpson': 0.16; 'subject:non': 0.16; 'wrote:': 0.16; 'string': 0.17; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'cheers,': 0.22; 'cc:no real name:2**0': 0.22; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; '(which': 0.26; 'bad.': 0.29; 'be:': 0.29; 'whitespace': 0.29; 'mention': 0.30; "i'd": 0.31; 'skip:. 10': 0.32; 'could': 0.35; 'text': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'basic': 0.36; 'faster': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'charset:us-ascii': 0.37; 'received:localdomain': 0.38; 'subject:-': 0.39; 'space': 0.40; 'maximum': 0.61; 'provide': 0.61; 'email addr:gmail.com': 0.62; 'more': 0.63; 'cameron': 0.66; 'elsewhere': 0.66; 'here': 0.66; 'subject:Check': 0.95
Content-Disposition inline
In-Reply-To <56857609.9080603@mrabarnett.plus.com>
User-Agent Mutt/1.5.24 (2015-08-30)
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>
Xref csiph.com comp.lang.python:101097

Show key headers only | View raw


On 31Dec2015 18:38, MRAB <python@mrabarnett.plus.com> wrote:
>On 2015-12-31 18:18, otaksoftspamtrap@gmail.com wrote:
>>I need to check a string over which I have no control for the first 2 non-white space characters (which should be '[{').
>>
>>The string would ideally be: '[{...' but could also be something like
>>'  [  {  ....'.
>>
>>Best to use re and how? Something else?
>>
>I would use .split and then ''.join:
>
>>>> ''.join(' [ { ....'.split())
>'[{....'

This presumes it is ok to drop/mangle/lose the whitespace elsewhere in the 
string. If it contains quoted text I'd expect that to be very bad.

>It might be faster if you provide a maximum for the number of splits:
>>>> ''.join(' [ { ....'.split(None, 1))
>'[{ ....'

Not to mention safer.

I would use lstrip and startswith:

  s = lstrip(s)
  if s.startswith('['):
    s = s[1:].lstrip()
    if s.startswith('{'):
      ... deal with s[1:] here ...

It is wordier, but far more basic and direct.

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

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


Thread

Re: Newbie: Check first two non-whitespace characters Cameron Simpson <cs@zip.com.au> - 2016-01-01 17:30 +1100

csiph-web