How to split comma-separated key-value pairs with quoted commas

后端 未结 5 1472
终归单人心
终归单人心 2021-01-02 13:03

I know there are a lot of other posts about parsing comma-separated values, but I couldn\'t find one that splits key-value pairs and handles quoted commas.

I have st

5条回答
  •  醉梦人生
    2021-01-02 13:52

    You just needed to use your shlex lexer in POSIX mode.

    Add posix=True when creating the lexer.

    (See the shlex parsing rules)

    lexer = shlex.shlex('''age=12,name=bob,hobbies="games,reading",phrase="I'm cool!"''', posix=True)
    lexer.whitespace_split = True
    lexer.whitespace = ','
    props = dict(pair.split('=', 1) for pair in lexer)
    

    Outputs :

    {'age': '12', 'phrase': "I'm cool!", 'hobbies': 'games,reading', 'name': 'bob'}
    

    PS : Regular expressions won't be able to parse key-value pairs as long as the input can contain quoted = or , characters. Even preprocessing the string wouldn't be able to make the input be parsed by a regular expression, because that kind of input cannot be formally defined as a regular language.

提交回复
热议问题