![](/img/trans.png)
[英]How would a range work if the starting point and ending points are the same in Python
[英]How to find a substring of text with a known starting point but unknown ending point in python
我有一长串文字。 我想使用关键字在某个点压缩该字符串,以指示我在 Python 中的新字符串的开始。 例如,我的字符串是:
“你好,我叫约翰。我今年二十五岁。我住在纽约市。我在华尔街工作。” 我想要从“纽约”到文本末尾的文本,即我需要代码来提取 substring“纽约市。我在华尔街工作。”
have = "Hello my name John. I am twenty-five years old. I live in New York City. I work on Wall Street."
want = "New York City. I work on Wall Street."
key_phrase = "New York"
任何帮助将非常感激!
我相信最好的方法是使用正则表达式:
import re
have = "Hello my name John. I am twenty-five years old. I live in New York City. I work on Wall Street."
want = "New York City. I work on Wall Street."
key_phrase = "New York"
key_phrase_begins = re.search(key_phrase, have).span()[0]
new_string = have[key_phrase_begins:]
print(new_string) # Outputs: 'New York City. I work on Wall Street.'
这样做是搜索您的 key_phrase,以及关键字在字符串中开始的索引 position。 然后它使用索引从原始字符串中 key_phrase 开始的位置创建新字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.