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


Groups > comp.lang.python > #10462

Re: shlex parsing

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <karim.liateni@free.fr>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.033
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; '>>>>': 0.09; 'arguments.': 0.09; 'subject:parsing': 0.09; 'tuple': 0.09; 'wrote:': 0.15; 'arguments:': 0.16; 'from:addr:free.fr': 0.16; 'received:free.fr': 0.16; 'skip:z 30': 0.16; 'splitted': 0.16; '\xe9crit': 0.16; 'pm,': 0.16; 'this:': 0.16; 'command': 0.19; 'header:In-Reply- To:1': 0.22; 'parse': 0.23; 'charset:iso-8859-15': 0.28; 'all,': 0.28; 'import': 0.29; 'gather': 0.29; 'value)': 0.30; 'thanks': 0.31; 'list:': 0.31; 'to:addr:python-list': 0.34; 'header:User- Agent:1': 0.34; 'pull': 0.37; 'received:192': 0.38; 'subject:: ': 0.38; 'two': 0.38; 'to:addr:python.org': 0.39; 'your': 0.60; 'feet': 0.64; 'beautiful': 0.77; 'dans': 0.77; '05:48': 0.84; '21:30': 0.84; 'off!': 0.84; 'so:': 0.84; 'hair': 0.91
Date Thu, 28 Jul 2011 18:21:49 +0200
From Karim <karim.liateni@free.fr>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Thunderbird/3.1.11
MIME-Version 1.0
To python-list@python.org
Subject Re: shlex parsing
References <mailman.1538.1311795072.1164.python-list@python.org> <4e3184d3$0$29530$426a74cc@news.free.fr>
In-Reply-To <4e3184d3$0$29530$426a74cc@news.free.fr>
Content-Type text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding 8bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1577.1311870117.1164.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311870117 news.xs4all.nl 23920 [2001:888:2000:d::a6]:54923
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10462

Show key headers only | View raw



Hello You have you feet on earth Web Dreamer!

Very clever!
Beautiful hack!

Many Thanks

Karim

On 07/28/2011 05:48 PM, Web Dreamer wrote:
> Karim a écrit ce mercredi 27 juillet 2011 21:30 dans<mailman.1538.1311795072.1164.python-list@python.org>  :
>
>> Hello All,
>>
>> I would like to parse this TCL command line with shlex:
>>
>> '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
>>
>> And I want to get the splitted list:
>>
>> ['-option1', '[get_rule A1 B2]', '-option2',  '$VAR', '-option3',  'TAG']
>>
>> Then I will gather in tuple 2 by 2 the arguments.
>
> Do this:
>
>>>> s = '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
> Now if you don't enclose [get_rule A1 B2] in cotes, you will pull your hair off!
> So:
>>>> s = s.replace('[','"[')
>>>> s = s.replace(']',']"')
> Now:
>>>> s
> '-option1 "[get_rule A1 B2]" -option2 $VAR -option3 TAG'
>
> Lets continue:
>>>> import shlex
>>>> optionlist = shlex.split(s)
>>>> optionlist
> ['-option1', '[get_rule A1 B2]', '-option2', '$VAR', '-option3', 'TAG']
>
> Now to get your tupple of two by two arguments:
>>>> argtuple = tuple([(option, value) for option,value in zip(optionlist[0::2],optionlist[1::2])])
>>>> argtuple
> (('-option1', '[get_rule A1 B2]'), ('-option2', '$VAR'), ('-option3', 'TAG'))
>
>
> whole code:
>>>> import shlex
>>>> s = '-option1 [get_rule A1 B2] -option2 $VAR -option3 TAG'
>>>> s = s.replace('[','"[')
>>>> s = s.replace(']',']"')
>>>> optionlist = shlex.split(s)
>>>> argtuple = tuple([(option, value) for option,value in zip(optionlist[0::2],optionlist[1::2]))

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


Thread

shlex parsing Karim <karim.liateni@free.fr> - 2011-07-27 21:30 +0200
  Re: shlex parsing Web Dreamer <webdreamer@nospam.fr> - 2011-07-28 17:48 +0200
    Re: shlex parsing Karim <karim.liateni@free.fr> - 2011-07-28 18:21 +0200
    Re: shlex parsing Nobody <nobody@nowhere.com> - 2011-07-28 17:37 +0100
      Re: shlex parsing Karim <karim.liateni@free.fr> - 2011-07-28 19:51 +0200
        Re: shlex parsing Web Dreamer <webdreamer@nospam.fr> - 2011-07-29 09:30 +0200
      Re: shlex parsing Web Dreamer <webdreamer@nospam.fr> - 2011-07-29 09:24 +0200
        Re: shlex parsing Karim <karim.liateni@free.fr> - 2011-07-29 11:37 +0200
          Re: shlex parsing Web Dreamer <webdreamer@nospam.fr> - 2011-07-29 15:42 +0200
            Re: shlex parsing Karim <karim.liateni@free.fr> - 2011-07-29 16:34 +0200

csiph-web