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' 
list(csv.reader([s]))[0]

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

1 Answers

0
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)
print(result)

['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.