Reputation: 3514
I am having trouble parsing String2, by a space. Thoughts?
String1 = "THIS IS STRING1 18-23-80-18"
String2 = "THIS IS STRING2 7-A-4, 4-93-P"
Split1 = String1.rsplit(" ",2)
Output1 = "18-23-80-18" #This Works fine
Split2 = String2.????? # Not sure what to do here
Output2 = "7-A-4, 4-93-P" #How do I Ignore the first space from the right?
Clarification:
The * represents the space I am trying to ignore and the % represents the space I want to find.
"%7-A-4,*4-93-P"
Thanks.
Upvotes: 1
Views: 5219
Reputation: 7285
If you are interested in splitting the string on the third left space and ignoring all others on the right then after:
>>> String2 = "THIS IS STRING2 7-A-4, 4-93-P"
>>> String2.split(" ", 3)[3]
'7-A-4, 4-93-P'
A general approach would be to split on spaces on the whole string and pick only from the range you want. Then join that range using a space.
" ".join(string.split()[1:3])
Upvotes: 5
Reputation: 298106
Use regex to split the string:
>>> import re
>>> string2 = "THIS IS STRING2 7-A-4, 4-93-P"
>>> re.split(r'[^,]\s+', string2)[-1]
'7-A-4, 4-93-P'
Upvotes: 1