如何从字符串中的字符串中找到字节位置,而不是字符位置?

How to find the byte position from a string in a string, not the character position?

我的文本编辑器 (vim) 可以给出字符串在字符串中的位置,但计算的是字节数,而不是字符数。

示例:

s="I don't take an apéritif après-ski"

当我搜索单词 apéritif 时,我的文本编辑器给出了位置:
16,25

Python给出同一个词的这个位置:
16,24

Vim 提供了在编辑器中执行 python 代码的可能性。
在我的一个 python 脚本中,我做了很多切片。
但是如果字符串中有重音字符,我永远找不到正确的词。
在 python 中有解决这个问题的方法吗?
我可以在python中的字符串中找到字符串的字节位置吗?

诚然,这是一个天真的解决方案。 您可以将文本和单词都编码为字节,然后 运行 以编码的单词为参数对编码的文本进行 find() 操作。

def f(text,word):
    en_text=bytes(text,encoding="utf-8")
    en_word=bytes(word,encoding="utf-8")
    start = en_text.find(en_word)
    return (start,start+len(en_word))

当运行为:

f("I don't take an apéritif après-ski","apéritif")

returns (16, 25)