Escaping commas within parens in CSV parsing?

felciano felciano at gmail.com
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"(.+?\([^\(\),]*?),(.+?\).*)")
	while pat.search(s):
		s = re.sub(pat,r"\1|\2",s)
	if trim:
		return [string.strip(string.replace(x,"|",",")) for x in
string.split(s,",")]
	else:
		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.

Ramon




More information about the Python-list mailing list