Search code examples
pythoncsvdata-cleaning

How do I convert "key=value, key=value, ..." to csv when some values contain the delimiter as part of the value?


I have a data set of the format key1=value1, key2=value2, key3=value3... where each key-value pair is separated from the others by a ", ".

However, some values are long strings that contain ", " as part of the value.

How can I correctly go through this data and convert it to csv?

I've tried using a csv.reader, but it doesn't work.

 data = row.lstrip('(').rstrip(')\n')                               
 reader = csv.reader(StringIO(data))                                
 for row2 in reader:                                                
     my_dict = {}                                                   
     for d in row2:                                                 
         my_dict[d.split('=')[0].lstrip()] = d.split('=', 1)[1]                                               

Solution

  • You can use re.findall with itertools.groupby:

    import re, itertools as it
    def get_vals(d):
       r = [(a, list(b)) for a, b in it.groupby(re.findall('\w+\=|[^\s,]+', d), key=lambda x:x[-1] == '=')]
       return {r[i][-1][0][:-1]:', '.join(r[i+1][-1]) for i in range(0, len(r), 2)}
    
    tests = ['key1=value1, key2=value2, key3=value3', 'key1=va, lue1, key2=valu, e2, test, key3=value3']
    print(list(map(get_vals, tests)))
    

    Output:

    [{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}, 
    {'key1': 'va, lue1', 'key2': 'valu, e2, test', 'key3': 'value3'}]