I have string ling like this:
'Toy Stroy..(II) (1995)'
I want to split the line to two parts like this:
['Toy Story..(II)','1995']
How can I do it? thanks
I have string ling like this:
'Toy Stroy..(II) (1995)'
I want to split the line to two parts like this:
['Toy Story..(II)','1995']
How can I do it? thanks
You could use regular expressions for that. See here: http://www.amk.ca/python/howto/regex/
Or you could use the split function and then manyally remove the parenthesis or other non desired characters. See here: http://www.python.org/doc/2.3/lib/module-string.html
This code will get you started:
'Toy Stroy..(II) (1995)'.rstrip(')').rsplit('(',1)
Other than that, you can use r'\s*[(]\d{4}[)]\s*$'
to match a four-digit number in parentheses at the end of the string. If you find it, you can chop it off:
s = ''
l = [s]
match = re.compile(r'\s*[(]\d+[)]\s*$').search(s)
if match is not None:
l = [s[:len(match.group(0))], s[-len(match.group(0)):].trim]
One way is this:
s = 'Toy Stroy..(II) (1995)'
print s[:s.rfind('(')].strip()
print s[s.rfind('('):].strip("()")
Output:
Toy Stroy..(II)
1995
>>>