I have a CSV file where one of the columns itself contains a list of words separated by commas. I want to take that list of words and replace the commas with a hyphen.
For example, if my list of words is ["Snow", "Wet"]
, and my line is:
value1, value2, Wet, Snow, value3
I'd like to have
value1, value2, Wet-Snow, value3
I almost know how to do this with sed, but it's giving me funky results. It seems ideal for a python regex, but I can't get it to work.
CodePudding user response:
Here's a split-and-loop solution:
words = ["Snow","Wet"]
def transform(src):
parts = src.split(", ")
out = []
for word in parts:
if word in words:
if second:
word = out.pop() '-' word
second = True
else:
second = False
out.append(word)
return ', '.join(out)
print( transform( "value1, value2, Wet, Snow, value3" ) )
Output:
value1, value2, Wet-Snow, value3
CodePudding user response:
IIUC, one way using itertools.groupby
:
from itertools import groupby
keys = {"Snow", "Wet"}
l = ['value1', 'value2', 'Wet', 'Snow', 'value3']
["-".join(g) for _, g in groupby(l, key=lambda x: (x in keys) or x)]
Output:
['value1', 'value2', 'Wet-Snow', 'value3']