Subtract strings

301
2
06-21-2011 09:59 PM
BartłomiejStaroń
New Contributor III
I have two columns of data with strings.
For example,
First colummn contains: xx
Second colummn contains : test xx

"test xx" - "xx" = "test"
Is it possible to with a python in a simple way?
Tags (2)
0 Kudos
2 Replies
KimOllivier
Occasional Contributor III
col1 = "test xx"
col2 = "xx"
print "<%s>\n" % col1.replace(col2,"").strip()


or use regular expressions to be much more precise, with the leading space and the xx at the end of the string.

import re
col1 = "test xx"
col2 = "xx"
pattern = re.compile(" ?xx$")
repl = ""
result = re.sub(pattern,repl,col1)
print "<%s> <%s> <%s>\n" % (col1,col2,result)

<test xx> <xx> <test>
0 Kudos
BartłomiejStaroń
New Contributor III
Thanks a lot. It works great.
0 Kudos