gpt4 book ai didi

python - 抓取字符串中 '\id' 之后的第一个单词

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:58 24 4
gpt4 key购买 nike

我如何获取字符串中 '\id ' 之后的第一个单词?

字符串:

'\id hello some random text that can be anything'

python

for line in lines_in:
if line.startswith('\id '):
book = line.replace('\id ', '').lower().rstrip()

我得到了什么

book = 'hello some random text that can be anything'

我想要什么

book = 'hello'

最佳答案

一个选项:

words = line.split()
try:
word = words[words.index("\id") + 1]
except ValueError:
pass # no whitespace-delimited "\id" in the string
except IndexError:
pass # "\id" at the end of the string

关于python - 抓取字符串中 '\id' 之后的第一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com