Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

split

python 2.7 code

cStr = '"aaaa","bbbb","ccc,ddd"' 
newStr = cStr.split(',')
print newStr  # -> ['"aaaa"','"bbbb"','"ccc','ddd"' ]

but, I want this result.

result = ['"aaa"','"bbb"','"ccc,ddd"'] 
like image 705
Jongpyo Jeon Avatar asked Mar 28 '17 10:03

Jongpyo Jeon


4 Answers

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 that delimiter , is followed by double quote "

like image 105
RomanPerekhrest Avatar answered Oct 12 '22 22:10

RomanPerekhrest


Try to use CSV.

import csv
cStr = '"aaaa","bbbb","ccc,ddd"'
newStr = [ '"{}"'.format(x) for x in list(csv.reader([cStr], delimiter=',', quotechar='"'))[0] ]

print newStr

Check Python parse CSV ignoring comma with double-quotes

like image 44
ghchoi Avatar answered Oct 12 '22 22:10

ghchoi


pyparsing has a builtin expression, commaSeparatedList:

cStr = '"aaaa","bbbb","ccc,ddd"' 
import pyparsing as pp
print(pp.commaSeparatedList.parseString(cStr).asList())

prints:

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

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,ddd']
like image 24
PaulMcG Avatar answered Oct 12 '22 23:10

PaulMcG


By using regex try this:

COMMA_MATCHER = re.compile(r",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)")
split_result = COMMA_MATCHER.split(string)

enter image description here

like image 35
Gosha null Avatar answered Oct 12 '22 22:10

Gosha null