【发布时间】:2021-06-17 12:42:56
【问题描述】:
问题:
我正在尝试找到一种将字符串、对象或文本文件转换为字典的方法,以便尝试将其转换为 sqlite 数据库。 (如果我以错误的方式处理此问题,我深表歉意。请随时推荐建议)
目前我有一个脚本可以生成我所有 mp4 元数据的列表,例如艺术家、歌曲、专辑、年份等:
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)
如果我使用text_file = open("sample.txt") tagfile = text_file.write(str(tag)) 和text_file.close() 的组合导出tag = TinyTag.get(musiclist),我将得到我的结果:
{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 332.06506666666667, "filesize": 550450310, "genre": null, "samplerate": 48000, "title": "27 Unknown", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive 2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 328.1945333333333, "filesize": 543487279, "genre": null, "samplerate": 48000, "title": "28 Unknown", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 372.80576666666667, "filesize": 617297262, "genre": null, "samplerate": 48000, "title": "Blazed Up Melpomene", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 191.2911, "filesize": 315409615, "genre": null, "samplerate": 48000, "title": "Bomb Factory - Exciter", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 388.75503333333336, "filesize": 645115333, "genre": null, "samplerate": 48000, "title": "Bomb Factory - Exciter2", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 82.71596666666667, "filesize": 132912355, "genre": null, "samplerate": 48000, "title": "D.O.A", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 258.19126666666665, "filesize": 425217111, "genre": null, "samplerate": 48000, "title": "Excelsior", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 342.8091333333333, "filesize": 567431045, "genre": null, "samplerate": 48000, "title": "Grand Style", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 335.70203333333336, "filesize": 556799644, "genre": null, "samplerate": 48000, "title": "Hitohira ~Reminiscent of Ketsui no Toko~", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 399.399, "filesize": 661114578, "genre": null, "samplerate": 48000, "title": "Jintsuriki", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 304.97133333333335, "filesize": 504466425, "genre": null, "samplerate": 48000, "title": "Natural High", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 327.8942333333333, "filesize": 542308702, "genre": null, "samplerate": 48000, "title": "Tehu Tehu", "track": null, "track_total": null, "year": "1999"}{"album": "Dead or Alive2", "albumartist": "Makoto Hosoi", "artist": "Makoto Hosoi", "audio_offset": null, "bitrate": 294651.393, "channels": 2, "comment": "Fighter", "composer": "Makoto Hosoi", "disc": null, "disc_total": null, "duration": 344.07706666666667, "filesize": 569536466, "genre": null, "samplerate": 48000, "title": "The Shooted 2", "track": null, "track_total": null, "year": "1999"}
# a lot more continued....
但是,如果我尝试将文本文件加载为 json 对象,以便尝试使用 sqlite 创建表,则会收到错误:
text_file = open("sample.txt")
line = text_file.read()
res = json.load(text_file)
结果:
ValueError: No JSON object could be decoded
我不需要先将数据转换为文本文件,但我需要找到将tag = TinyTag.get(musiclist) 转换为字典的方法,以便我可以试试这个:how to create a sqlite3 table from a dictionary
# i am assuming this is what I will need but I need to make my data into a dictionary first
conn = sqlite3.connect('musicdatabase.db')
c = conn.cursor()
for tablist in res.keys():
fieldset = []
for col, definition in res[tablist].items():
fieldset.append("'{0}' {1}".format(col, definition))
if len(fieldset) > 0:
query = "CREATE TABLE IF NOT EXISTS {0} ({1})".format(tablist, ", ".join(fieldset))
c.execute(query)
c.close()
conn.close()
如果有人有任何建议或尝试不同的方法,请告诉我。
感谢您的宝贵时间,
【问题讨论】:
-
您没有构建有效的 JSON。您在没有任何分隔符的情况下在同一行上编写多个 JSON。这是“几乎”
ndjson,其中每一行都是有效的 JSON,但它们必须位于单独的行上,之后您仍然需要处理 line bi 行。如果你想要一个有效的 JSON - 你需要制作将转换为对象数组的字典列表。
标签: python mysql json dictionary