[Tutor] Editing values from a dictionary

Bob Williams linux at barrowhillfarm.org.uk
Wed Feb 26 18:09:49 CET 2014


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi List,

I have two problems, but it's possible that one solution will suffice.
I am using a module called mutagen to extract audio metadata from
.flac files. The output of mutagen is in the form of a dictionary, so

In [1]: import mutagen.flac

In [2]: metadata = mutagen.flac.Open("/home/bob/music/artists/The
Incredible String Band/1967 The 5000 Spirits Or The Layers Of The
Onion/08 The Hedgehog's Song.flac")

In [3]: print metadata["artist"]
[u'The Incredible String Band']

I now want to pass that string to another program, but I want to strip
off the leading [u' and the trailing ']. However, this doesn't work:

In [4]: artistName = metadata["artist"][3:-2]

In [5]: print artistName
[]

I was expecting The Incredible String Band, not []

What am I doing wrong? Or what have I misunderstood?

The other problem concerns the program that receives these arguments -
it complains (and stops with an error) if one the arguments is empty.
For example, the flac file may not have the date information:

Traceback (most recent call last):
  File "/home/bob/Documents/scripts/python/flac2mp3v2.py", line 81, in
<module> subprocess.call(['lame', '--add-id3v2',
'--ignore-tag-errors', '--tt', str(metadata['title']), '--ta',
str(metadata['artist']), '--tl', str(metadata['album']), '--ty',
str(metadata['date']), '--tn', str(metadata['tracknumber']), '--tg',
str(metadata['genre']), tempName1, tempName3])
  File "/usr/lib/python2.7/site-packages/mutagen/__init__.py", line
85, in __getitem__
    else: return self.tags[key]
  File "/usr/lib/python2.7/site-packages/mutagen/_vorbis.py", line
184, in __getitem__
    if not values: raise KeyError, key
KeyError: 'date'

If it's possible to edit the string value that gets passed to
subprocess.call('lame'...) - see problem #1 above, would it also be
possible to define a default value if the original field is empty?

Bob
- -- 
Bob Williams
System:  Linux 3.11.10-7-desktop
Distro:  openSUSE 13.1 (x86_64) with KDE Development Platform: 4.12.2
Uptime:  12:00pm up 13 days 20:00, 6 users, load average: 0.10, 0.19, 0.26
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlMOH9sACgkQ0Sr7eZJrmU5ufACeILRlmiXt4CgDa6ZpdTI3Npm5
FToAn2+AcjNKGxJKU+9nE9IdsoEqlQdd
=JpdC
-----END PGP SIGNATURE-----


More information about the Tutor mailing list