如何在一组特定的文本后找到字符串?

2024-05-14 20:03:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从下面的源代码中的\u id字段后面捕获24个字符的字符串:

[{"actors":"Natalie Portman, Hugo Weaving, Stephen Rea","year":2006,"description":"","title":"V for Vendetta","image":"http:\/\/content8.flixster.com\/movie\/11\/16\/67\/11166734_det.jpg","rating":3.65,"_id":"4eb04794f5f8077d1d000000","links":{"rottentomatoes":"http:\/\/www.rottentomatoes.com\/m\/v_for_vendetta\/","imdb":"http:\/\/www.imdb.com\/title\/tt0434409\/","shortUrl":"http:\/\/www.canistream.it\/search\/movie\/4eb04794f5f8077d1d000000\/v-for-vendetta"}},{"actors":"Guy Madison, Monica Randall, Mariano Vidal Molina","year":1966,"description":"","title":"I Cinque della vendetta (Five for Revenge)(The Five Giants from Texas)(No Drums No Trumpets)","image":"http:\/\/images.rottentomatoescdn.com\/images\/redesign\/poster_default.gif","rating":-0.05,"_id":"4e663229f5f8071702000002","links":{"imdb":"http:\/\/www.imdb.com\/title\/tt0060238\/","rottentomatoes":"http:\/\/www.rottentomatoes.com\/m\/i-cinque-della-vendetta-five-for-revengethe-five-giants-from-texasno-drums-no-trumpets\/","shortUrl":"http:\/\/www.canistream.it\/search\/movie\/4e663229f5f8071702000002\/i-cinque-della-vendetta-five-for-revenge-the-five-giants-from-texas-no-drums-no-trumpets-"}}]

我试过使用如下的lookback,但是没有成功。你知道吗

^(?<=_id":")[a-z0-9]{24}

我把它作为Python脚本的一部分来使用,如果它有区别的话。你知道吗


Tags: nofromcomidhttpfortitlewww
3条回答

如果上面的数据是存储在变量中的json对象,比如data

data[0]['_id'] 

给你想要的。你知道吗

如果它是一个字符串,则使用python的jsonmodule将其作为json加载,并按上述方式访问数据,即

import json
data_j = json.loads(data)
data_j[0]['_id'] 

像其他两个答案一样,如果您有原始的数据结构,请使用它们。但如果所有这些都失败了,这或许可以奏效:

pat = '_id":"'
i = s.find(pat)
if i >= 0:
    i += len(pat)
value = s[i:i+24]

这是一个list,里面有一个dictionary,如果它叫做D

>>> D[0]['_id']
   '4eb04794f5f8077d1d000000'

相关问题 更多 >

    热门问题