1

I have string looking like this:

'Toy Story..(II) (1995)'

I want to split the line into two parts like this:

['Toy Story..(II)','1995']

How can I do it? Thanks.

4
  • 1
    Is that the only string you want to split? If not, could you give a few more examples? Commented Nov 11, 2009 at 8:52
  • The reason I'm asking is that if that is the only string you want to split, I would just store it in its final form: l = ['Toy Story..(II)' '1995'] Commented Nov 11, 2009 at 8:54
  • 1
    It will be difficult to automatically do the conversion from "Stroy" to "Story" for similar spelling errors, but apart from that, some simple string parsing or a simple regex will help. Commented Nov 11, 2009 at 9:03
  • -1: The title is useless. Please change "what I want" to something that could be helpful to the thousands of people who look for help on this site every day. Commented Nov 11, 2009 at 11:13

4 Answers 4

4

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]
Sign up to request clarification or add additional context in comments.

2 Comments

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?
Try it, it works since the regexp matches just one four-digit number at the end of the string.
1

One way is this:

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

Output:

Toy Stroy..(II)
1995
>>> 

Comments

0

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

2 Comments

The line may be complex, and It will be difficult to design a regular to work well.
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.
0
l[:-1].split("(")

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.