Escaping commas within parens in CSV parsing?

felciano felciano at
Fri Jul 1 19:18:15 CEST 2005

Thanks for all the postings. I can't change delimiter in the source
itself, so I'm doing it temporarily just to handle the escaping:

def splitWithEscapedCommasInParens(s, trim=False):
	pat = re.compile(r"(.+?\([^\(\),]*?),(.+?\).*)")
		s = re.sub(pat,r"\1|\2",s)
	if trim:
		return [string.strip(string.replace(x,"|",",")) for x in
		return [string.replace(x,"|",",") for x in string.split(s,",")]

Probably not the most efficient, but its "the simplest thing that
works" for me :-)

Thanks again for all the quick responses.


More information about the Python-list mailing list