Given a string of words separated by whitespace. Need to replace whitespaces with comma ignoring whitespaces in quotes.
>>> some_string = 'one two "three four" five "six seven"'
>>> replace_func(some_string)
'one,two,"three four",five,"six seven"'
Here is the simple decision:
def replace_func(some_str):
lines = []
i = 1
for l in struct.split('"'):
if i % 2:
lines.append(l.replace(' ', ',')
else:
lines.append(l)
i += 1
parsed_struct = '"'.join(lines)
Any suggestions?
This can be easily done with the help of
shlex.split:I you need to preserve quotation marks you can do this: