【发布时间】:2021-03-28 14:20:56
【问题描述】:
问题
我想将字典合并到我使用 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'}]
当我希望 path 作为 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)
任何想法或建议将不胜感激
【问题讨论】:
-
请提供预期的MRE - Minimal, Reproducible Example。显示中间结果与预期结果的偏差。我们应该能够将您的代码块粘贴到文件中,运行它并重现您的问题。这也让我们可以在您的上下文中测试任何建议。我们期望这个问题的最小工作示例,包括跟踪内部操作的适当代码。您发布的代码无法运行,因为它依赖于私有文件。
-
如果你希望它是一本字典,你应该使用
dict.update -
这段代码远非最小。当您的帖子强制水平滚动时,您会失去读者。修复格式以适合页面。更好的是,缩短示例。
-
乍一看,问题似乎是你
appended 了一些你想成为update的东西。阅读文档以了解差异,或者只是通过有关 dicts 及其方法的教程。
标签: python list dictionary