954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help splitting 3part string into 3part list

I'm having difficulties splitting up a string (that I read from a textfile) into three parts in a list (that I will then use in a function).

The issue I'm having is that the 3rd element of the string is not always one word (sometimes its multiple words, but I want them to be counted as ONE)
ex for one line:
1200 90 Class
ex for another line:
1900 120 Date Night

I can get the first instance to work just fine by using split() on my string, but when I try the second instance, I get '1900' , '120', 'Date', 'Night'...when I want it to read '1900', '120', 'Date Night'.

Is there a way to specify the split to be a space for the first two parts of the string and then the third part is everything up until the new line?

Or how how should I go about this?

Thank you

paraclete
Newbie Poster
4 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

You can use a second argument to split

>>> "1900 120 Date Night".split(" ", 2)
['1900', '120', 'Date Night']

Otherwise, use a regular expression (the re module).

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: