Norman Khine: > I have a csv file which is has a field that has something like: > "text (xxx)" > "text (text) (yyy)" > "text (text) (text) (zzz)" > > I would like to split the last '(text)' out and put it in a new column, > so that I get: > "text","(xxx)" > "text (text)","(yyy)" > "text (text) (text)","(zzz)"
Maybe something like this can be useful, after few improvements (RE formatting is a work in progress): from StringIO import StringIO import re datain = StringIO(""" "text (xxx)" "text (text) (yy y) " "text (text) (text) ( zzz ) " """) lastone = re.compile(""" \s* ( \( [^()"]* \) \s* " ) \s* $ """, re.VERBOSE) def repl(mobj): txt_found = mobj.groups()[0] return '", "' + txt_found for line in datain: line2 = line.strip() if line2: print lastone.sub(repl, line2) """ The output is: "text", "(xxx)" "text (text)", "(yy y) " "text (text) (text)", "( zzz ) " """ Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list