parse_keyval

privex.helpers.common.parse_keyval(line: str, valsplit: str = ':', csvsplit=',') → List[Tuple[str, str]][source]

Parses a csv with key:value pairs such as:

John Alex:Doe,Jane Sarah:Doe

Into a list with tuple pairs (can be easily converted to a dict):

[
    ('John Alex', 'Doe'), 
    ('Jane Sarah', 'Doe')
]

By default, uses a colons : to split the key/value, and commas , to terminate the end of each keyval pair. This can be overridden by changing valsplit/csvsplit.

Parameters
  • line (str) – A string of key:value pairs separated by commas e.g. John Alex:Doe,Jane Sarah:Doe

  • valsplit (str) – A character (or several) used to split the key from the value (default: colon :)

  • csvsplit (str) – A character (or several) used to terminate each keyval pair (default: comma ,)

Return List[Tuple[str,str]] parsed_data

A list of (key, value) tuples that can easily be casted to a dict()