【问题标题】:convert string or text file to dictionary python (No JSON object could be decoded)将字符串或文本文件转换为字典 python(不能解码 JSON 对象)
【发布时间】: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


【解决方案1】:

生成的 json 对象有错误。您发布的 json 不是有效的 json 对象,这就是您无法解析它的原因。

对于一个有效的 json 对象,你应该:

  1. 你的json文件以[开头

  2. 确保每个对象之间有一个,

  3. ]结束你的json文件

还想我可能会分享一种更有效的用 python 打开文件的方法。

import json

with open('sample.json', 'r') as sample:
    json.load(sample)

如果我们以这种方式打开文件,我们可以避免在读/写后忘记关闭文件。运行其中的代码后,它会为您关闭文件。

【讨论】:

  • 在这种情况下,我可能会找到一种不同的方式来做到这一点。也许我可以将tag = TinyTag.get(musiclist) 转换为字典?我会将此标记为答案,因为我很确定我需要寻找不同的方法。再次感谢!
  • @JatonJameerJustice 您可以在每个对象之后添加这些字符,并通过简单地编写字符来以[] 开始和结束文件。尽管在这种情况下您可能希望找到更好更可靠的方法。
  • 有超过 6000 首歌曲。我只是导出到一个文本文件,试图找到一种方法将它放入 sqlite,这样我就可以尝试对数据进行搜索查询。我无法进入该文本文件并对其进行编辑。我将不得不尝试不同的方法。不过好主意!
  • @JatonJameerJustice 我的意思是以编程方式将它们附加到文件中。在开始解析元数据之前添加[,在每个对象之后添加,,在完成收集每个项目之后,在关闭文件之前添加]
  • 我不知道这怎么可能。 [] 是有道理的,但我不知道如何在每个结果之间添加 ,tag 同时给我所有结果的列表。我不知道如何打破它。我会假设tag_str = str("{" + tag + "]") 但我不知道, 应该去哪里。我想我会通过尝试将 tag = TinyTag.get(musiclist) 转换为 dict 来解决这个问题,因为我不知道这将如何使用文本文件。
猜你喜欢
  • 1970-01-01
  • 2017-04-27
  • 2019-04-02
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多