anyone can help with below?

(shlex.split or re could work, but no idea why codes below won't work)

s = 'hello, world, a, "b,c", d' 

# ['hello', ' world', ' a', ' "b', 'c"', ' d'] - get this
# ['hello', ' world', ' a', 'b,c', ' d'] - i want this

1 Answers

Tim Biegeleisen On

For the exact sample data you showed us, using re.split on the pattern ,\s+ would work:

s = 'hello, world, a, "b,c", d'
result = re.split(r',\s+', s)

['hello', 'world', 'a', '"b,c"', 'd']

This answer hinges on that the CSV data contained inside double quotes would not have any whitespace along with the comma separator.