0

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

as it marked as duplicated, but found the link can't answer the question, especially below which still some problem for quotation in csv.reader:

s3 = "self, c: hug.types.number, d='hello, world'"
list(csv.reader([s3], skipinitialspace=True))[0]

# ['self', 'c: hug.types.number', "d='hello", "world'"] - get this
# ['self', 'c: hug.types.number', "d='hello, world'"] - i want this
Felix Liu
  • 199
  • 2
  • 9

1 Answers1

0

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360