我目前有一段代码,用于解析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
我在想:有没有更好的方法来做我所缺少的?在
您可以尝试在删除所有不必要的头和尾字符后将所有数据转换为dict。在
我使用regex,但是它很容易被任何方法替换,在检索到的字符串中最多删除{“and after}”。在
在我看来每个人都工作得太辛苦了。。。如果你真的
看起来像一个字符串。。。在
然后从字符串前面去掉“unicode:”
^{pr2}$然后将结果直接评估成dict
然后通过键访问数据
您有完美的格式来创建Python dict,并且可以直接从该容器访问值。在
相关问题 更多 >
编程相关推荐