如何使用 Python 和 Mutagen 访问这些 ID3 值 [英] How do I access these ID3 values using Python and Mutagen
问题描述
我正在尝试从我庞大的卡拉 OK 收藏中读取 ID3 标签,以便我可以将它们存储在 CSV 文件中,以便于格式化和打印,但我在实际获取值时遇到了问题!如果我打印方法 items()
:
I'm trying to read the ID3 tags from my vast karaoke collection, so that I can store them in a CSV file, for easy formatting and printing, but I'm having issues actually getting the values! I manage to get them in a format like this, if I print the method items()
:
[('artist', [u'Steve Miller Band']), ('title', [u'Space Cowboy [Karaoke]'])][('artist', [u'Stevie Wonder']), ('title', [u"Livin' For The City [Karaoke]"])]
我使用的代码如下:
#! /usr/bin/env python2.7
import os
import glob
import os.path
import csv
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
#form stagger.id3 import *
directory = os.path.normpath("C:/Users/Adrian/Music/Karaoke/Kar/*/*.cdg")
files = glob.glob(directory)
csvw = open('C:\\python\\karaoke.csv', 'wb')
info = []
for f in files:
split = os.path.splitext(f)
newpath = split[0] + ".mp3"
if(os.path.isfile(newpath)):
audio = MP3(newpath, ID3=EasyID3)
stuff = audio.items()
print stuff
try:
print>>csvw, (stuff[0] + ", " + stuff[1] + "\r\n")
except:
print>>csvw, (os.path.basename(newpath) + "\r\n")
info.append(audio.items())
我尝试使用整数来访问它们,就好像它们是一个列表一样,也尝试使用字符串来访问它们,就好像它是一本字典,但我每次都遇到错误.我在 SE 某处读到函数 items()
返回一个类似字典的对象",但在谷歌上搜索也没有帮助我!
I have tried using integers to access them as if they were a list, and also strings as if it were a dictionary, but I get an error each time. I've read somewhere on SE that the function items()
returns a "dictionary-like-object", but searching on google hasn't helped me either!
推荐答案
我设法解决了这个问题,用了几个小时和足够多的抽烟休息时间!如果有人在开始用 Python 编程时遇到这个问题,这是我找到的解决方案:
I managed to fix this problem, with a few hours and more than enough cig breaks! If anyone come across this problem when starting to program with Python, this is the solution I found:
#! /usr/bin/env python2.7
import os
import glob
import os.path
import csv
import string
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
#form stagger.id3 import *
directory = os.path.normpath("C:/Users/Adrian/Music/Karaoke/Kar/*/*.cdg")
files = glob.glob(directory)
csvw = open('C:\\python\\karaoke.csv', 'wb')
info = []
for f in files:
split = os.path.splitext(f)
newpath = split[0] + ".mp3"
if(os.path.isfile(newpath)):
audio = MP3(newpath, ID3=EasyID3)
stuff = audio.items()
artist = ""
titley = ""
for stu in stuff:
i = 0
for s in stu:
if (s == "artist"):
artist = filter(lambda x: x in string.printable, ''.join(stu[(i+1)]))
artist = artist.encode('ascii',errors='ignore')
if (s == "title"):
titley = filter(lambda x: x in string.printable, ''.join(stu[(i+1)]))
titley = titley.encode('ascii',errors='ignore')
i = i+1
if(artist != "" and titley != ""):
print>>csvw, (artist + ", " + titley + "\r\n")
事实证明,我试图获取的数据类型是多维元组,我仍然不完全确定它们与列表之间的区别是什么!但是您似乎无法像访问其他语言中的数组那样访问它们的值,例如.value[a][b]
,但一些嵌套的 for 循环似乎在我的情况下成功了!在写入 CSV 文件时,我也遇到了带有重音符号的 UTF 编码字符的一些问题,因此需要过滤器.
It turns out that the data type I was trying to get was a multi-dimensional tupal, which I'm still no entirely sure what the difference between them and a list is! But you don't seem to be able to access their values as you would an array in other languages eg. value[a][b]
, but some nested for loops seem to have done the trick in my case! I also had some problems with UTF encoding characters with accents when writing to a CSV file, hence the need for the filters.
由于我发现的唯一一款可以解决我遇到的特定问题的软件(为卡拉 OK 文件制作歌曲列表)的成本在 40-60 英镑之间,或者免费软件对卡拉 OK 库的限制非常低,我将努力制作这个开源,具有基本的 GUI,所以我欢迎任何人可能想要添加的任何改进!
Since the only software I have found that solve the particular problem I had (Making song lists for karaoke files) cost between £40-60, or the free ones have stupidly low limits for a karaoke library, I will work on making this open source, with a basic GUI, SO I welcome any improvements anyone may like to add!
这篇关于如何使用 Python 和 Mutagen 访问这些 ID3 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!