I am trying to find things in a string – all of them are before a number, for example:
"Diablo Lord Of Destruction 9.2"
This is an index from a file such that file[2] = "Diablo Lord Of Destruction 9.2"
how can I write code that will select only the text and leave out the numbers and any white space before those numbers (as below)?
"Diablo Lord Of Destruction"
I understand you can easily do this by doing something like this:
contents = file[2]
print contents[0:-2]
Since the values will be changing, I need a more robust solution that can handle different sized numbers and different lengths of white space.
You can utilize regular expressions and the sub() method:
The code above will find all number occurrences, [0-9.] or [\d.], and replace them with ”. In addition, it trims the last character, which was a space.