On Sep 2, 12:46 am, Tim Arnold <a_j...@bellsouth.net> wrote:
> Hi,
> I have a set of strings that are *basically* comma separated, but with
> the exception that if a comma occur insides curly braces it is not a
> delimiter.  Here's an example:
>
> [code=one, caption={My Analysis for \textbf{t}, Version 1}, continued]
>
> I'd like to parse that into a dictionary (note that 'continued' gets
> the value 'true'):
> {'code':'one', 'caption':'{My Analysis for \textbf{t}, Version
> 1}','continued':'true'}
>
> I know and love pyparsing, but for this particular code I need to rely
> only on the standard library (I'm running 2.7). Here's what I've got,
> and it works. I wonder if there's a simpler way?
> thanks,
> --Tim Arnold
>
> The 'line' is like my example above but it comes in without the ending
> bracket, so I append one on the 6th line.
>


You can use regular expression (also you not need adding ending
bracket):

import re
patt = re.compile(ur'\[code=(?P<CODE>\w+),\scaption=(?P<CAPTION>\{.+\})
(?P<CONTINUED>,\scontinued)?\]?')
def parse_options(s):
        g=patt.match(s).groupdict()
        return {'caption' : g['CAPTION'], 'code' : g['CODE'], 'continued' :
g['CONTINUED'] and True or False}


Test is next:


>>> s=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}, continued]'
>>> s1=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}]'
>>> s2=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}, continued'
>>> s3=u'[code=one, caption={My Analysis for \textbf{t}, Version 1}'

>>> parse_options(s)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': True}
>>> parse_options(s1)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': False}
>>> parse_options(s2)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': True}
>>> parse_options(s3)
{'caption': u'{My Analysis for \textbf{t}, Version 1}', 'code':
u'one', 'continued': False}
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to