问题
I am currently trying to use Python and its eyeD3 library to extract the lyrics from an mp3 file. The lyrics have been embedded into the mp3 file already (via: MusicBee). I am trying to use eyeD3 to return the lyrics. I can't figure out how to do it. I have searched online extensively and all I've found were tutorials showing how to SET the lyrics. I just want to read them from the file. Here's my current code:
track = eyed3.load(path)
tag = track.tag
artist = tag.artist
lyrics = tag.lyrics
artist returns the artist's name correctly but lyrics returns the following:
<eyed3.id3.tag.LyricsAccessor object at 0x27402d0>
How can I just return the raw text lyrics embedded into an mp3? Is this possible?
Thank you so much in advanced.
回答1:
It looks like that is a iterator. Try
tag.lyrics[0]
or
for lyric in tag.lyrics:
print lyric
last resort print the objects directory and look for useful functions
print dir(tag.lyrics)
回答2:
u"".join([i.text for i in tag.lyrics])
回答3:
DonJuma, Python tells you:
<iterator object at hex_location>
You can try the following but it fails on strings
hasattr(myObj, '__iter__')
user:mindu explains here that you can write a robust function that checks
try:
some_object_iterator = iter(some_object)
except TypeError, te:
print some_object, 'is not iterable'
BUT!!! This is not Pythonic. Python believes in Duck Typing which basically says, "If it looks like a duck and quacks like a duck, it must be a duck." See the link for more info.
来源:https://stackoverflow.com/questions/16182094/retrieve-lyrics-from-an-mp3-file-in-python-using-eyed3