Skip to content
Advertisement

Can’t access full iteration of dictionary outside of for loop python

The problem

I am trying to access my tag_dict dictionary variable outside of a for loop so I can use that dictionary to learn how to insert that data into sqlite. (First Sqlite project)

If I print the tag_dict inside of the for loop it gives me all the data.

Here is the code:

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

Here is the result if I add a print tag_dict to the for loop:

{'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

Here is the result if I add a print tag_dict to the outside of the for loop:

{'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? :(
>>> 

Any ideas would be appreciated.

Thank you for your time,

Advertisement

Answer

On each iteration of the inner loop, tag_dict = tag.as_dict() is setting tag_dict to a new value. So, after the loop is finished, the variable only has the final value, and none of the previous values.

If you want to keep all of the values, you could use a list instead, and append tag.as_dict() to that list within the loop.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement