views:

142

answers:

4

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

A: 

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

Konamiman
The line may be complex, and It will be difficult to design a regular to work well.
Charlie Epps
If the line always ends with the year, a simple regex with something like (.*)\s*\((\d{4})\) should do the trick. You'll have the name of the movie in the first backreference and the year in the second.
Kaivosukeltaja
A: 
l[:-1].split("(")
coulix
Thanks, I'm sorry that I give a simple example. But in real word, the line would be like this "Toy Stoy..(II) (1995)". In this case, your method will not work well.
Charlie Epps
+1  A: 

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]
Aaron Digulla
For your second method, I think it is not suitable for my problem. You see, if the sting is "Toy Stoy(1000) (1995)". the method won't work well. do you think so?
Charlie Epps
Try it, it works since the regexp matches just one four-digit number at the end of the string.
Aaron Digulla
+1  A: 

One way is this:

s = 'Toy Stroy..(II) (1995)'
print s[:s.rfind('(')].strip()
print s[s.rfind('('):].strip("()")

Output:

Toy Stroy..(II)
1995
>>>
Nick D
good work, thank you very much
Charlie Epps