簡體   English   中英

將字典合並到列表問題 - python

[英]Merging dictionary into list issues - python

問題

我想將字典合並到我使用 For 循環生成的列表的末尾。 它似乎有效,但問題是花括號留在字典周圍而不是與列表合並。 這有點難以解釋,所以這里有兩個例子:

這就是我想要的(抱歉,您必須向右滾動才能看到path鍵值):

[{'comment': u'Fighter', 'album': u'Dead or Alive2', 'audio_offset': None, 
'title': u'You Are Under My Control ~Beautiful Version00~', 'track': None, 
'disc_total': None, 'artist': u'Makoto Hosoi', 'track_total': None, 'channels': 2, 'genre': None, 'albumartist': u'Makoto Hosoi', 'filesize': 669796508L, 
'composer': u'Makoto Hosoi', 'year': u'1999', 'duration': 403.26953333333336, 'samplerate': 48000, 'bitrate': 294651.393, 'disc': None, 
'path': '\\\\Vgmstation\\\\e\\\\Dreamcast\\\\Dead or Alive 2\\You Are Under My Control ~Beautiful Version00~.mp4'}]

這是正在發生的事情:

[{'comment': u'Fighter', 'album': u'Dead or Alive2', 'audio_offset': None, 'title': u'You Are Under My Control ~Beautiful Version00~', 'track': None, 'disc_total': None, 'artist': u'Makoto Hosoi', 'track_total': None, 'channels': 2, 'genre': None, 'albumartist': u'Makoto Hosoi', 'filesize': 669796508L, 'composer': u'Makoto Hosoi', 'year': u'1999',
 'duration': 403.26953333333336, 'samplerate': 48000, 'bitrate': 294651.393, 'disc': None}, 
{'path': '\\\\Vgmstation\\\\e\\\\Dreamcast\\\\Dead or Alive 2\\You Are Under My Control ~Beautiful Version00~.mp4'}]

請注意,當我希望它作為disc之后的最后一個鍵值時,大括號是如何圍繞path鍵的

這是我的相關代碼:

import os
import subprocess
from tinytag import TinyTag
import json

tag = ''
tag_dicts = []
extf = ['$RECYCLE.BIN']
for root, dirs, files in os.walk(r'\\Vgmstation\\e\\Dreamcast\\Dead or Alive 2', followlinks=True):
    dirs[:] = [d for d in dirs if d not in extf]
    for name in files:
        if name.endswith(".mp4"):
            musiclist=str(os.path.join(root, name))
            tag = TinyTag.get(musiclist)
            musicpath_dict = {'path': os.path.join(root,name)}
            musiccopy = musicpath_dict.copy()
            tag_dicts.append(tag.as_dict())
            tag_dicts.append(musiccopy)


print(tag_dicts)

任何想法或建議將不勝感激

而不是附加到作為列表的tag_dicts ,您應該更新列表中的字典。 所以而不是下面:

tag_dicts.append(tag.as_dict())
tag_dicts.append(musiccopy)

使用以下內容更新字典:

tag_dicts.append(tag.as_dict())
tag_dicts[0].update(musiccopy)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM