-2

How would I isolate the numbers before "chaos" and "exalted" when the words may change in this string?

@From ProhtienArc: Hi, I'd like to buy your 69.5 chaos for my 1 exalted in Incursion.

I'm also using sikuli if there's a way to do it with that.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
Navajo
  • 15
  • 2
  • 1
    I can't understand what your question means. Can you explain more details? – Hoseong Jeon Jul 18 '18 at 09:54
  • I would like to check what the numbers are before chaos and exalted even if the words change to something like alchemy or fusing and the numbers change as well. Sorry if this is still confusing I don't know how to explain it well. – Navajo Jul 18 '18 at 10:03
  • Oh I understand now. Thanks. :) – Hoseong Jeon Jul 18 '18 at 10:10
  • Actually, now I don't understand. Do you know in advance what you are looking for (the words the numbers are before) or do the other words stay these same and these could be anything? Maybe show three or four example sentences so I can see what stays fixed and what doesn't. Or do you just want to pull any numbers out of a sentence and note what words come afterwards? – doctorlove Jul 18 '18 at 10:17
  • Currencies = ('chaos', 'alchemy', 'exalted', 'alteration', 'fusing', "jeweller's", 'none') are the words im choosing from but the numbers can change and the words can be in different order – Navajo Jul 18 '18 at 10:30

1 Answers1

0

There are many approaches. The simplest (which will explode if the words you look for aren't there):

Given a string:

s="@From ProhtienArc: Hi, I'd like to buy your 69.5 chaos for my 1 exalted in Incursion."

Split it (on spaces):

L=s.split()

You can then search the list

['@From', 'ProhtienArc:', 'Hi,', "I'd", 'like', 'to', 'buy', 'your', '69.5', 'chaos', 'for', 'my', '1', 'exalted', 'in', 'Incursion.']

like this

>>> L.index("chaos")
9

and use that index to find the token before:

>>> L[L.index("chaos")-1]
'69.5'

This is a string so you then need to convert it to a number.

Do likewise with the other word. Watch out for not finding the words or them being at the initial index.

And more generally, you could use any word in place if the "chaos".

def number_before(word, split_words):
    return L[L.index(word)-1]

Split you string and call it:

as_string = number_before("chaos", s.split())
number = float(as_string)

You need to decide what to do if the word isn't there.

doctorlove
  • 18,872
  • 2
  • 46
  • 62