Split by comma and how to exclude comma from quotes in split ... Python -


python 2.7 code

cstr = '"aaaa","bbbb","ccc,ddd"'   newstr = cstr.split(',')  print newstr   # result : ['"aaaa"','"bbbb"','"ccc','ddd"' ] 

but, want result.

result = ['"aaa"','"bbb"','"ccc,ddd"']  

help..

the solution using re.split() function:

import re  cstr = '"aaaa","bbbb","ccc,ddd"' newstr = re.split(r',(?=")', cstr)  print newstr 

the output:

['"aaaa"', '"bbbb"', '"ccc,ddd"'] 

,(?=") - lookahead positive assertion, ensures delimiter , followed double quote "


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -