Norman Khine wrote: > Hello, > I have a csv file which is has a field that has something like: > > text.csv > "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: > > new_test.csv > "text","(xxx)" > "text (text)","(yyy)" > "text (text) (text)","(zzz)" > > how can this be done?
line.rsplit(None, 1) seems to do the trick for me: >>> tests=[ ... ("text (xxx)", ("text","(xxx)")), ... ("text (text) (yyy)", ("text (text)","(yyy)")), ... ("text (text) (text) (zzz)", ("text (text) (text)","(zzz)")) ... ] >>> for test, result in tests: ... r = test.rsplit(None,1) ... if r[0] <> result[0] or r[1] <> result[1]: ... print test, result ... shows that the results of rsplit() match the expected results. -tkc -- http://mail.python.org/mailman/listinfo/python-list