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


Groups > comp.lang.python > #101075

Re: Newbie: Check first two non-whitespace characters

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Karim <kliateni@gmail.com>
Newsgroups comp.lang.python
Subject Re: Newbie: Check first two non-whitespace characters
Date Thu, 31 Dec 2015 20:05:02 +0100
Lines 38
Message-ID <mailman.119.1451588711.11925.python-list@python.org> (permalink)
References <240ab049-68a0-4a00-b911-d58cae9bfbcf@googlegroups.com> <568579DF.50805@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de +ts7NnDuvk0vjphFPftIvgC0T/Nw1yknqDEpo68/I+oA==
Return-Path <kliateni@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'ideally': 0.04; 'line:': 0.07; 'subject:two': 0.07; 'subject:characters': 0.09; 'forward:': 0.16; 'parse,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:non': 0.16; 'wrote:': 0.16; 'string': 0.17; '>>>': 0.20; 'to:2**1': 0.21; 'doc': 0.22; 'parse': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; '(which': 0.26; 'to:no real name:2**1': 0.27; 'be:': 0.29; 'depth': 0.29; 'print': 0.30; 'message-id:@gmail.com': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'replace': 0.35; 'something': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'list.': 0.37; 'received:192': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'space': 0.40; 'your': 0.60; "you'll": 0.61; 'hope': 0.61; 'email addr:gmail.com': 0.62; 'charset:windows-1252': 0.62; 'subject:Check': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=LAcM/j40+6euPUkYMBhiVA3VOBOmTBXcYboAESkKhcg=; b=xP+NRkIS2lVW0uOhBgUgQ/eKu04VErQZpbIDDqdwYc/DS3hWpCcg1NPzw5U3YpZj/d a3wdBJTp5ftK4z3kCe5xE5rUILE+85nz4P+jI2V+k1sf3S0yzBuzf+J30VAlLwljwl4e git1rlbluL6ql4BDJEZUtZpZqL3FAfMIMSMc5V8eInO+yCnO7R5UnEln/JVeRAqedUyq q+DXogU109dFFJxKp4PAm0G2qDD7KuEz4F4jbcqq0wMoGiQJoKcJ5kLPA6qMA9PlOED+ hrBcpp9t5kAJKBQbQU27nHK50t9JN33YyU97q61nmzDZwcHTbqY+idmWYct9M0kMGdds PDlw==
X-Received by 10.28.47.213 with SMTP id v204mr66753579wmv.69.1451588704197; Thu, 31 Dec 2015 11:05:04 -0800 (PST)
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0
In-Reply-To <568579DF.50805@gmail.com>
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:101075

Show key headers only | View raw



On 31/12/2015 19:54, Karim wrote:
>
>
> On 31/12/2015 19: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?
>
> Use pyparsing it is straight forward:
>
> >>> from pyparsing import Suppress, restOfLine
>
> >>> mystring = Suppress('[') + Suppress('{') + restOfLine
>
> >>> result = mystring.parse(' [ { .... I am learning pyparsing' )
>
> >>> print result.asList()
>
> ['.... I am learning pyparsing']
>
> You'll get your string inside the list.
>
> Hope this help see pyparsing doc for in depth study.
>
> Karim

Sorry the method to parse a string is parseString not parse, please 
replace by this line:

 >>> result = mystring.parseString(' [ { .... I am learning pyparsing' )

Regards

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


Thread

Newbie: Check first two non-whitespace characters otaksoftspamtrap@gmail.com - 2015-12-31 10:18 -0800
  Re: Newbie: Check first two non-whitespace characters MRAB <python@mrabarnett.plus.com> - 2015-12-31 18:38 +0000
  Re: Newbie: Check first two non-whitespace characters Karim <kliateni@gmail.com> - 2015-12-31 19:54 +0100
  Re: Newbie: Check first two non-whitespace characters Karim <kliateni@gmail.com> - 2015-12-31 20:05 +0100
    Re: Newbie: Check first two non-whitespace characters cassius.fechter@gmail.com - 2015-12-31 11:21 -0800
  Re: Newbie: Check first two non-whitespace characters Cory Madden <csmadden@gmail.com> - 2015-12-31 10:56 -0800
  Re: Newbie: Check first two non-whitespace characters Denis McMahon <denismfmcmahon@gmail.com> - 2015-12-31 20:35 +0000
  Re: Newbie: Check first two non-whitespace characters Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-31 23:25 +0000
    Re: Newbie: Check first two non-whitespace characters Steven D'Aprano <steve@pearwood.info> - 2016-01-01 12:12 +1100
  Re: Newbie: Check first two non-whitespace characters Steven D'Aprano <steve@pearwood.info> - 2016-01-01 12:23 +1100
  Re: Newbie: Check first two non-whitespace characters Random832 <random832@fastmail.com> - 2015-12-31 20:31 -0500
  Re: Newbie: Check first two non-whitespace characters Jussi Piitulainen <harvesting@is.invalid> - 2016-01-01 10:16 +0200
  Re: Newbie: Check first two non-whitespace characters Karim <kliateni@gmail.com> - 2016-01-01 10:43 +0100

csiph-web