如何检查 python 中字符串中单词前的字符是什么

How to check what a character is before a word in a string in python

当字符串中的单词可能发生变化时,我如何隔离 "chaos""exalted" 之前的数字?

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

如果有办法的话,我也在使用 sikuli。

方法有很多种。 最简单的(没有你要找的字会爆炸):

给定一个字符串:

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

拆分(按空格):

L=s.split()

然后您可以搜索列表

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

像这样

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

并使用该索引查找之前的令牌:

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

这是一个字符串,因此您需要将其转换为数字。

另一个词也一样。 当心找不到单词或它们在初始索引处。

更一般地说,如果 "chaos".

,您可以在适当的位置使用任何词
def number_before(word, split_words):
    return L[L.index(word)-1]

拆分你的字符串并调用它:

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

如果该词不存在,您需要决定要做什么。