Python Split Command Separated Key Value Pair With Quotes

July 3, 2019

Scenario 1: perfectly formatted value

import shlex

value_str = 'name="Desmond Lua",age=40,gender=M,friends="James, Ramesh, K\'to, 小爱"'

# posix=True - remove quotes of string in final value
s = shlex.shlex(value_str, posix=True)
s.whitespace_split = True
s.whitespace = ', \n'
data = dict(pair.split('=', 1) for pair in s)
{
    'name': 'Desmond Lua',
    'age': '40',
    'gender': 'M',
    'friends': "James, Ramesh, K'to, 小爱"
}

Scenario 2: whitespace and comma as separator

import shlex

value_str = 'name="Desmond Lua",\tage=40, gender=M,\nfriends="James, Ramesh, K\'to, 小爱"'

s = shlex.shlex(value_str, posix=True)
s.whitespace_split = True
s.whitespace = ', \n\t'
data = dict(pair.split('=', 1) for pair in s)

NOTE: This solution doesn’t work if the is space in key-value pair (e.g. name = "Desmond Lua")

Scenario 3: spaces in key value pair

import shlex
import re

value_str = 'name = "Desmond Lua" ,\tage=40, gender=M,\nfriends="James, Ramesh, K\'to, 小爱"'

s = shlex.shlex(value_str, posix=True)
s.whitespace_split = True
s.whitespace = ','

p = re.compile(r'\s*=\s*')
data = dict(p.split(pair.strip(), maxsplit=1) for pair in s)

NOTE: This assume the actual key/value doesn’t have whitespace on left and right.

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.