Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Split by comma and how to exclude comma from quotes in 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"'] 
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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 "

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report

0

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']
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!