使用额外值更新dict

时间:2015-01-03 11:17:03

标签: python dictionary

我有以下内容将数据(show)从1种格式转换为另一种格式,这种情况一直有效,直到trakt.tv更新了它们的api。

我现在需要的是每个季节内的所有剧集编号,如同

"seasons": [{"number": 1, "episodes": [{"number": 1}, {"number": 2}]}]}]

转换后的数据最终应该是

"shows": [{"title": "The Walking Dead", "year": 2010, "ids": {"tvdb": 153021, "imdb": "tt1520211", "tmdb": 1402, "tvrage": 25056}, "seasons": [{"number": 1, "episodes": [{"number": 1}, {"number": 2}]}]}]

原始数据(显示)看起来像

{'episodes': [{'episode': 0, 'season': 1}, {'episode': 1, 'season': 1}, {'episode': 2, 'season': 1}], 'imdbnumber': '260449', 'title': 'Vikings'}

标题,年份和ids部分都可以,如果我现在运行代码,我最终会有多个第1季(每集1个)

def convert_YAMJ_show_to_trakt(show):
#"shows": [{"title": "The Walking Dead", "year": 2010, "ids": {"tvdb": 153021, "imdb": "tt1520211", "tmdb": 1402, "tvrage": 25056}, "seasons": [{"number": 1, "episodes": [{"number": 1}, {"number": 2}]}]}]
trakt_show = {'ids':{}}

if 'imdbnumber' in show:
    if show['imdbnumber'].startswith('tt'):
        trakt_show['ids']['imdb'] = show['imdbnumber']
    else:
        trakt_show['ids']['tvdb'] = show['imdbnumber']

if 'tvdb' in show:
    trakt_show['ids']['tvdb'] = show['tvdb']

if 'imdb' in show:
    trakt_show['ids']['imdb'] = show['imdb']

if 'title' in show:
    trakt_show['title'] = show['title']

if 'episodes' in show and show['episodes']:
    for episode in show['episodes']:
        if 'date' in episode:
            #ep = {'episode': episode['episode'], 'season': episode['season'], 'watched_at': episode['date']}
            ep = {"seasons": [{"number": episode['season'], "episodes": [{"number": episode['episode']}]}]}
        else:
            #ep = {'episode': episode['episode'], 'season': episode['season']}
            ep = {"seasons": [{"number": episode['season'], "episodes": [{"number": episode['episode']}]}]}

        #if 'playcount' in episode:
        #     ep['plays'] = episode['playcount']

        #trakt_show['seasons'].append(ep)
        trakt_show.append(ep)
return trakt_show

1 个答案:

答案 0 :(得分:1)

你可以试试这个: -

  1. 我改变了循环逻辑。已创建dictionary key season numbervaluelist的剧集编号。
  2. 从字典中最终创建的输出结构。
  3. 代码:

    show = {
            'episodes': [
                        {'episode': 0, 'season': 1}, {'episode': 1, 'season': 1}, {'episode': 2, 'season': 1},
                        {'episode': 0, 'season': 2}, {'episode': 1, 'season': 2}, {'episode': 2, 'season': 2},
                    ], 
                'imdbnumber': '260449', 'title': 'Vikings'
            }
    
    
    trakt_show = {'ids':{}}
    
    if 'imdbnumber' in show:
        if show['imdbnumber'].startswith('tt'):
            trakt_show['ids']['imdb'] = show['imdbnumber']
        else:
            trakt_show['ids']['tvdb'] = show['imdbnumber']
    try:
        trakt_show['ids']['tvdb'] = show['tvdb']
    except:
        pass
    try:
        trakt_show['ids']['imdb'] = show['imdb']
    except:
        pass
    try:
        trakt_show['title'] = show['title']
    except:
        pass
    
    if 'episodes' in show and show['episodes']:
        ep = {}
        for episode in show['episodes']:
            try:
                ep[episode["season"]].append(episode["episode"])
            except:
                ep[episode["season"]] = [episode["episode"]]
    
        ep1 = {"seasons":[]}
        for i in ep:
            tmp = dict()
            tmp["number"] = i
            tmp["episodes"] = []
            for j in ep[i]:
                tmp["episodes"].append({"number":j})
            ep1["seasons"].append(tmp)
        trakt_show.update(ep1)
    print trakt_show
    

    输出:

    vivek@vivek:~/Desktop$ python 2.py 
    {'seasons': [{'episodes': [{'number': 0}, {'number': 1}, {'number': 2}], 'number': 1}, {'episodes': [{'number': 0}, {'number': 1}, {'number': 2}], 'number': 2}], 'ids': {'tvdb': '260449'}, 'title': 'Vikings'}