【发布时间】: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