Regular expression to match comma separated list of key=value where value can contain commas

前端 未结 5 692
一生所求
一生所求 2020-12-20 15:54

I have a naive \"parser\" that simply does something like:
[x.split(\'=\') for x in mystring.split(\',\')]

However mystring can be something like<

5条回答
  •  无人及你
    2020-12-20 16:28

    Can you try this, it worked for me:

    mystring = "foo=bar,breakfast=spam,eggs,e=a"
    n = []
    i = 0
    
    for x in mystring.split(','):
        if '=' not in x:
            n[i-1] = "{0},{1}".format(n[i-1], x)
        else:
            n.append(x)
            i += 1
    print n
    

    You get result like:

      ['foo=bar', 'breakfast=spam,eggs', 'e=a']
    

    Then you can simply go over list and do what you want.

提交回复
热议问题