突变键错误
Mutagen Key Error
下面的代码尝试使用诱变剂从 mp3 文件中获取标签,然后使用诱变剂 api 自动更新不存在的标签。当我们尝试更新不存在的标签时,问题就来了。任何帮助,将不胜感激。我不精通 python 语言,因为我 6 个月前才开始编码。
#import mutagen
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
def readid3mp3(ip):
audio = MP3(ip)
if ((audio['TPE1']) == 0):
tags.add = ID3()
tags.add(TPE1(encoding=3, text =["Not Available"]))
else:
Art = (audio['TPE1'])
if ((audio['TIT2']) == 0):
tags.add =ID3()
tags.add(TIT2(encoding=3, text ="Not Available"))
else:
Song = (audio['TIT2'])
if ((audio['TALB']) is None):
tags.add = ID3()
tags.add(TALB(encoding=3, text ="Not Available"))
audio = MP3(ip)
Art = (audio['TPE1'])
Song = (audio['TIT2'])
Alb = (audio['TALB'])
print(audio['TIT2'])
print(audio['TPE1'])
print(audio['TALB'])
#Print into file
myFile.write ("\n"+"Artist"+"\t \t \t Song Name" + "\t Album Name" )
myFile.write ("\n"+ str(Art) + "\t" + str(Song) + "\t" + str(Alb))
#Close File
myFile.close()
return
#Open File
myFile = open("AlbArtSong.txt", "a+")
#Enter Inputs
ip = input("Enter Mp3 file: ")
readid3mp3(ip)
"Not Available" 是暂时的,直到我整理好 api
这是我得到的错误
Traceback (most recent call last):
File "C:\Users\patri_000\Desktop\Uni Stuff\EE106\Progect\test.py", line 59, in <module>
readid3mp3(ip)
File "C:\Users\patri_000\Desktop\Uni Stuff\EE106\Progect\test.py", line 23, in readid3mp3
if ((audio['TALB']) is None):
File "C:\Python34\lib\site-packages\mutagen\_file.py", line 54, in __getitem__
return self.tags[key]
File "C:\Python34\lib\site-packages\mutagen\_util.py", line 202, in __getitem__
return self.__dict[key]
KeyError: 'TALB'
我知道这个错误是什么意思我只是不知道如何绕过它或修复它
像这样支持订阅的诱变剂对象就像 Python dictionaries. Use a membership test to see if the key is there, or use the .get()
method 到 return 如果缺少条目则为默认值:
if 'TALB' in audio:
Alb = audio['TALB']
或
Alb = audio.get('TALB') # returns None as a default
或
Alb = audio.get('TALB', 'No album title') # alternative default
下面的代码尝试使用诱变剂从 mp3 文件中获取标签,然后使用诱变剂 api 自动更新不存在的标签。当我们尝试更新不存在的标签时,问题就来了。任何帮助,将不胜感激。我不精通 python 语言,因为我 6 个月前才开始编码。
#import mutagen
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
def readid3mp3(ip):
audio = MP3(ip)
if ((audio['TPE1']) == 0):
tags.add = ID3()
tags.add(TPE1(encoding=3, text =["Not Available"]))
else:
Art = (audio['TPE1'])
if ((audio['TIT2']) == 0):
tags.add =ID3()
tags.add(TIT2(encoding=3, text ="Not Available"))
else:
Song = (audio['TIT2'])
if ((audio['TALB']) is None):
tags.add = ID3()
tags.add(TALB(encoding=3, text ="Not Available"))
audio = MP3(ip)
Art = (audio['TPE1'])
Song = (audio['TIT2'])
Alb = (audio['TALB'])
print(audio['TIT2'])
print(audio['TPE1'])
print(audio['TALB'])
#Print into file
myFile.write ("\n"+"Artist"+"\t \t \t Song Name" + "\t Album Name" )
myFile.write ("\n"+ str(Art) + "\t" + str(Song) + "\t" + str(Alb))
#Close File
myFile.close()
return
#Open File
myFile = open("AlbArtSong.txt", "a+")
#Enter Inputs
ip = input("Enter Mp3 file: ")
readid3mp3(ip)
"Not Available" 是暂时的,直到我整理好 api
这是我得到的错误
Traceback (most recent call last):
File "C:\Users\patri_000\Desktop\Uni Stuff\EE106\Progect\test.py", line 59, in <module>
readid3mp3(ip)
File "C:\Users\patri_000\Desktop\Uni Stuff\EE106\Progect\test.py", line 23, in readid3mp3
if ((audio['TALB']) is None):
File "C:\Python34\lib\site-packages\mutagen\_file.py", line 54, in __getitem__
return self.tags[key]
File "C:\Python34\lib\site-packages\mutagen\_util.py", line 202, in __getitem__
return self.__dict[key]
KeyError: 'TALB'
我知道这个错误是什么意思我只是不知道如何绕过它或修复它
像这样支持订阅的诱变剂对象就像 Python dictionaries. Use a membership test to see if the key is there, or use the .get()
method 到 return 如果缺少条目则为默认值:
if 'TALB' in audio:
Alb = audio['TALB']
或
Alb = audio.get('TALB') # returns None as a default
或
Alb = audio.get('TALB', 'No album title') # alternative default