Search code examples
pythonregexstring-parsingbrackets

how to turn a string of letters embedded in squared brackets into embedded lists


I'm trying to find a simple way to convert a string like this:

a = '[[a b] [c d]]'

into the corresponding nested list structure, where the letters are turned into strings:

a = [['a', 'b'], ['c', 'd']]

I tried to use

import ast
l = ast.literal_eval('[[a b] [c d]]')
l = [i.strip() for i in l]

as found here

but it doesn't work because the characters a,b,c,d are not within quotes.

in particular I'm looking for something that turns:

'[[X v] -s]'

into:

[['X', 'v'], '-s']

Solution

  • >>> import json
    >>> a = '[[a b] [c d]]'
    >>> a = ''.join(map(lambda x: '"{}"'.format(x) if x.isalpha() else x, a))
    >>> a
    '[["a" "b"] ["c" "d"]]'
    >>> json.loads(a.replace(' ', ','))
    [[u'a', u'b'], [u'c', u'd']]
    

    This will work with any degree of nested lists following the above pattern, e.g.

    >>> a = '[[[a b] [c d]] [[e f] [g h]]]'
    >>> ...
    >>> json.loads(a.replace(' ', ','))
    [[[u'a', u'b'], [u'c', u'd']], [[u'e', u'f'], [u'g', u'h']]]
    

    For the specific example of '[[X v] -s]':

    >>> import json
    >>> a = '[[X v] -s]'
    >>> a = ''.join(map(lambda x: '"{}"'.format(x) if x.isalpha() or x=='-' else x, a))
    >>> json.loads(a.replace('[ [', '[[').replace('] ]', ']]').replace(' ', ',').replace('][', '],[').replace('""',''))
    [[u'X', u'v'], u'-s']