gpt4 book ai didi

python : string split with either A or B

转载 作者:行者123 更新时间:2023-11-28 21:33:13 24 4
gpt4 key购买 nike

我有:

 s='"Tag":"Football","name":"Mickael A","Played":"10times","Tag":"Basket","name":"Bruce B","Played":"8times","Tag":"Football","name":"John R","Played":"6times",'

我想根据足球和篮球进行分割,即:

['','"Mickael A","Played":"10times"',
'"Bruce B","Played":"8times",',
'"John R","Played":"6times",']

我尝试过:

s.strip().split(r'"Tag":("Football"|"Basket"),"name":')

但是它不起作用。

最佳答案

您需要的是使用 re 库并使 Football 和篮球非捕获组,这样它们就不会出现在结果中,如下所示:

import re
re.split(r'"Tag":(?:"Football"|"Basket"),"name":', s)

结果将是:

['', '"Mickael A","Played":"10times",', '"Bruce B","Played":"8times",', '"John R","Played":"6times",']

关于 python : string split with either A or B,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54928777/

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