python 2.7 code
cStr = \'\"aaaa\",\"bbbb\",\"ccc,ffffd\"\'
newStr = cStr.split(\',\')
print newStr
# result : [\'\"aaaa\"\',\'\"bbbb\"\',\'\"ccc\',\'ffffd\
You can first split the string by "
then filter out ''
or ','
, finally format it, it may be the simplest way:
['"%s"' % s for s in cStr.split('"') if s and s != ',']
This is not a standard module, you have to install it via pip, but as an option try tssplit:
In [3]: from tssplit import tssplit
In [4]: tssplit('"aaaa","bbbb","ccc,ffffd"', quote='"', delimiter=',')
Out[4]: ['aaaa', 'bbbb', 'ccc,ffffd']
pyparsing has a builtin expression, commaSeparatedList
:
cStr = '"aaaa","bbbb","ccc,ffffd"'
import pyparsing as pp
print(pp.commaSeparatedList.parseString(cStr).asList())
prints:
['"aaaa"', '"bbbb"', '"ccc,ffffd"']
You can also add a parse-time action to strip those double-quotes (since you probably just want the content, not the quotation marks too):
csv_line = pp.commaSeparatedList.copy().addParseAction(pp.tokenMap(lambda s: s.strip('"')))
print(csv_line.parseString(cStr).asList())
gives:
['aaaa', 'bbbb', 'ccc,ffffd']
By using regex try this:
COMMA_MATCHER = re.compile(r",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)")
split_result = COMMA_MATCHER.split(string)
The solution using re.split() function:
import re
cStr = '"aaaa","bbbb","ccc,ffffd"'
newStr = re.split(r',(?=")', cStr)
print newStr
The output:
['"aaaa"', '"bbbb"', '"ccc,ffffd"']
,(?=")
- lookahead positive assertion, ensures that delimiter ,
is followed by double quote "
Try to use CSV.
import csv
cStr = '"aaaa","bbbb","ccc,ffffd"'
newStr = [ '"{}"'.format(x) for x in list(csv.reader([cStr], delimiter=',', quotechar='"'))[0] ]
print newStr
Check Python parse CSV ignoring comma with double-quotes