iter
iter

Reputation: 4313

Parsing name-value pairs with quoted values in Python

I need to parse strings of name-value pairs. The values can be quoted strings, presumably so that their values can contain separator characters. Example:

Path="/5E13F93E/5E142BAA" Ref="U4"  Part="1"

Funkier example:

Path="/5E13F93E= 5E14 2BAA" Ref="U4"  Part="1"

Is there a standard way to do this in Python? I can't imagine writing my own character-level parser for this in 2020, but I can't immediately find anything in the standard library that does this.

Upvotes: 0

Views: 53

Answers (1)

chepner
chepner

Reputation: 531055

Assuming this is supposed to be a series of POSIX variable assignments, you can use the shlex module:

>>> import shlex
>>> list(shlex.shlex('Path="/5E13F93E= 5E14 2BAA" Ref="U4"  Part="1"'))
['Path', '=', '"/5E13F93E= 5E14 2BAA"', 'Ref', '=', '"U4"', 'Part', '=', '"1"']

You'll have to remove the outer quotes from the values yourself, but that can get a little tricky. Leading quotes are easy to identify: if the first character of a value is " or ', remove it. However, if the last value is a quote, you need to check if it is escaped first (e.g., foo=bar\"

Upvotes: 2

Related Questions