Python中字符串的高效解析

2024-05-17 13:32:57 发布

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

我目前有一段代码,用于解析imdbAPI http返回:

text = 'unicode: {"Title":"The Fountain","Year":"2006","Rated":"R","Released":"22 Nov 2006","Genre":"Drama, Romance, Sci-Fi","Director":"Darren Aronofsky","Writer":"Darren Aronofsky, Darren Aronofsky","Actors":"Hugh Jackman, Rachel Weisz, Sean Patrick Thomas, Ellen Burstyn","Plot":"Spanning over one thousand years, and three parallel stories, The Fountain is a story of love, death, spirituality, and the fragility of our existence in this world.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTU5OTczMTcxMV5BMl5BanBnXkFtZTcwNDg3MTEzMw@@._V1_SX320.jpg","Runtime":"1 hr 36 mins","Rating":"7.4","Votes":"100139","ID":"tt0414993","Response":"True"}'

def stripData(tag="Title"):
    tag_start = text.find(tag)
    data_start = tag_start + len(tag)+3
    data_end = text.find('"',data_start)
    data = text[data_start:data_end]
    return tag, data  

我在想:有没有更好的方法来做我所缺少的?在


Tags: andofthe代码texthttpdatatitle
3条回答
>>> ast.literal_eval(text.split(' ', 1)[1])
{'Plot': 'Spanning over one thousand years, and three parallel stories, The Fountain is a story of love, death, spirituality, and the fragility of our existence in this world.', 'Votes': '100139', 'Rated': 'R', 'Response': 'True', 'Title': 'The Fountain', 'Poster': 'http://ia.media-imdb.com/images/M/MV5BMTU5OTczMTcxMV5BMl5BanBnXkFtZTcwNDg3MTEzMw@@._V1_SX320.jpg', 'Writer': 'Darren Aronofsky, Darren Aronofsky', 'ID': 'tt0414993', 'Director': 'Darren Aronofsky', 'Released': '22 Nov 2006', 'Actors': 'Hugh Jackman, Rachel Weisz, Sean Patrick Thomas, Ellen Burstyn', 'Year': '2006', 'Genre': 'Drama, Romance, Sci-Fi', 'Runtime': '1 hr 36 mins', 'Rating': '7.4'}

>>> json.loads(text.split(' ', 1)[1])
{u'Plot': u'Spanning over one thousand years, and three parallel stories, The Fountain is a story of love, death, spirituality, and the fragility of our existence in this world.', u'Votes': u'100139', u'Rated': u'R', u'Response': u'True', u'Title': u'The Fountain', u'Poster': u'http://ia.media-imdb.com/images/M/MV5BMTU5OTczMTcxMV5BMl5BanBnXkFtZTcwNDg3MTEzMw@@._V1_SX320.jpg', u'Writer': u'Darren Aronofsky, Darren Aronofsky', u'ID': u'tt0414993', u'Director': u'Darren Aronofsky', u'Released': u'22 Nov 2006', u'Actors': u'Hugh Jackman, Rachel Weisz, Sean Patrick Thomas, Ellen Burstyn', u'Year': u'2006', u'Genre': u'Drama, Romance, Sci-Fi', u'Runtime': u'1 hr 36 mins', u'Rating': u'7.4'}

您可以尝试在删除所有不必要的头和尾字符后将所有数据转换为dict。在

import re

line = 'unicode: {"Title":"The Fountain","Year":"2006","Rated":"R","Released":"22 Nov 2006","Genre":"Drama, Romance, Sci-Fi","Director":"Darren Aronofsky","Writer":"Darren Aronofsky, Darren Aronofsky","Actors":"Hugh Jackman, Rachel Weisz, Sean Patrick Thomas, Ellen Burstyn","Plot":"Spanning over one thousand years, and three parallel stories, The Fountain is a story of love, death, spirituality, and the fragility of our existence in this world.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTU5OTczMTcxMV5BMl5BanBnXkFtZTcwNDg3MTEzMw@@._V1_SX320.jpg","Runtime":"1 hr 36 mins","Rating":"7.4","Votes":"100139","ID":"tt0414993","Response":"True"}'

def parser(text):
    match = re.search(r'\{\"([^}]+)\"\}', text)
    if match:
        return dict(x.split('":"') for x in match.group(1).split('","'))

newdict = parser(line)

for k, v in newdict.items():
    print k, v

我使用regex,但是它很容易被任何方法替换,在检索到的字符串中最多删除{“and after}”。在

在我看来每个人都工作得太辛苦了。。。如果你真的

line = 'unicode: {"key1":"Value1", "key2","value2", etc...}'

看起来像一个字符串。。。在

然后从字符串前面去掉“unicode:”

^{pr2}$

然后将结果直接评估成dict

data_dict=eval(newline)

然后通过键访问数据

print(data_dict['Title'])

您有完美的格式来创建Python dict,并且可以直接从该容器访问值。在

相关问题 更多 >