Split with python

Tim Chase python.list at tim.thechases.com
Tue Aug 29 14:27:34 EDT 2006


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







More information about the Python-list mailing list