【问题标题】:Can't access full iteration of dictionary outside of for loop python无法在for循环python之外访问字典的完整迭代
【发布时间】:2021-03-21 01:01:29
【问题描述】:

问题

我正在尝试在 for 循环之外访问我的 tag_dict 字典变量,以便我可以使用该字典来学习如何将该数据插入到 sqlite 中。 (第一个 Sqlite 项目)

如果我在 for 循环中打印 tag_dict,它会给我所有数据。

代码如下:

import os
import subprocess
from tinytag import TinyTag
import sqlite3
import json


tag = ''
extf = ['$RECYCLE.BIN','System Volume Information']
for root, dirs, files in os.walk(r'\\Vgmstation\\e\\', 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)
            tag_dict = tag.as_dict()
            #print tag_dict   - This prints everything but I want this outside of the for loop...or how am I going to insert this into my sqlite database?

#print tag_dict   - This prints only one iteration when I need all the data so I can pass it over to an INSERT command in sqlite.... I think

如果我在 for 循环中添加 print tag_dict,结果如下:

{'comment': u"Shoot 'em up", 'album': u'Aerostar', 'audio_offset': None, 'title': u'Stage 7', 'track': None, 'disc_total': None, 'artist': u'Dota Ando', 'track_total': None, 'channels': 2, 'genre': u'8-Bit', 'albumartist': u'Dota Ando', 'filesize': 160102674L, 'composer': u'Dota Ando', 'year': u'1991', 'duration': 99.9999, 'samplerate': 48000, 'bitrate': 294651.393, 'disc': None}
{'comment': u'Platformer', 'album': u'Aladin', 'audio_offset': None, 'title': u'Track 10', 'track': None, 'disc_total': None, 'artist': u'Steve Rockett', 'track_total': None, 'channels': 2, 'genre': u'8-Bit', 'albumartist': u'Steve Rockett', 'filesize': 302885452L, 'composer': u'Steve Rockett', 'year': u'2000', 'duration': 180.04653333333334, 'samplerate': 48000, 'bitrate': 294651.393, 'disc': None}
{'comment': u'Platformer', 'album': u'Aladin', 'audio_offset': None, 'title': u'Track 11', 'track': None, 'disc_total': None, 'artist': u'Steve Rockett', 'track_total': None, 'channels': 2, 'genre': u'8-Bit', 'albumartist': u'Steve Rockett', 'filesize': 302791162L, 'composer': u'Steve Rockett', 'year': u'2000', 'duration': 180.04653333333334, 'samplerate': 48000, 'bitrate': 294651.393, 'disc': None}
#there is over 6000 more tracks to get through

如果我将print tag_dict 添加到 for 循环的外部,结果如下:

{'comment': u'Action-adventure', 'album': u'Spawn Armageddon', 'audio_offset': None, 'title': None, 'track': '9', 'disc_total': None, 'artist': u'Rik Schaffer', 'track_total': '0', 'channels': 2, 'genre': None, 'albumartist': u'Rik Schaffer', 'filesize': 75479673L, 'composer': u'Rik Schaffer', 'year': u'2003', 'duration': 49.11573333333333, 'samplerate': 48000, 'bitrate': 294651.393, 'disc': None}
#and that's it. where is the rest of my data? :(
>>> 

任何想法都将不胜感激。

感谢您的宝贵时间,

【问题讨论】:

    标签: python dictionary for-loop variables


    【解决方案1】:

    在内循环的每次迭代中,tag_dict = tag.as_dict()tag_dict 设置为一个新值。所以,循环结束后,变量只有最终值,没有之前的值。

    如果您想保留所有值,您可以改用一个列表,并将tag.as_dict() 附加到循环内的该列表中。

    【讨论】:

    • 我尝试这样做,但收到错误AttributeError: 'dict' object has no attribute 'append'。我尝试点击此链接:kite.com/python/answers/…,但没有多大帮助。你知道如何使用附加吗?我从未使用过它,它似乎不适用于字典。
    • tag_dict 是字典而不是列表,所以你不能在上面调用append。我推荐的是在循环上方添加tag_dicts = [],然后用tag_dicts.append(tag.as_dict()) 替换当前的tag_dict = tag.as_dict() 行。然后,您将获得所有字典的列表,而不仅仅是一个字典。
    • 我“认为”这是答案,所以我将其标记为正确。我“认为”它的答案是因为当我尝试运行脚本时,它被冻结了一段时间。我有超过 6000 首歌曲,我认为将元数据从一个对象转换为字典,再转换为列表需要一段时间。
    • 嗯,这很奇怪,我不认为将其更改为使用列表会减慢速度
    • 实际上你的代码可以工作,它就是那么大。哈哈哈我停止了代码并将链接更改为特定的导向器路径,因此我得到的结果更少,而且它在几秒钟内就完全出现了。大声笑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2020-04-08
    • 2016-09-04
    • 1970-01-01
    • 2019-07-22
    相关资源
    最近更新 更多