I'm trying to split a sentence into three different variables for later use, and I need to specify some rules which will split it in a way I need.
EXAMPLE SENTENCE:
sentence = 'SUPER Jumper Colour BROWN-8'
From this I need three Variables
textBeforeColour = 'SUPER Jumper Colour'
Colour = 'BROWN'
Size = '8'
PS. the Colour (BROWN-8) will always be in CAPS Anything before the colour (BROWN-8) might have some words in CAPS but not all.
I've created a script that will do just that but I know that if the text changes slightly the script will break. For example
import re
text = 'SUPER Jumper Colour BROWN-8'
list = text.split()
myList = []
lastWord = list[-1]
for iterating_var in list:
if iterating_var is not list[-1]: #THIS GIVES ME THE 'BEFORE COLOUR' TEXT
myList.append(iterating_var)
if lastWord == 'SIZE':
print('ONE SIZE') #This is used when the Size is not a number but comes as ONE SIZE
else:
splitText = re.split('-',lastWord)
print(splitText[0])
print(splitText[1])
Colour = splitText[0]
size = splitText[1]
Now all of this works. But if the string will use a colour: LIGHT BLUE - this script will keep the 'LIGHT' with the sentence variable not with the colour Variable.