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


Groups > comp.lang.python > #10462

Re: shlex parsing

Date 2011-07-28 18:21 +0200
From Karim <karim.liateni@free.fr>
Subject Re: shlex parsing
References <mailman.1538.1311795072.1164.python-list@python.org> <4e3184d3$0$29530$426a74cc@news.free.fr>
Newsgroups comp.lang.python
Message-ID <mailman.1577.1311870117.1164.python-list@python.org> (permalink)

Show all headers | 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