[Tutor] Editing values from a dictionary
Krishnan Shankar
i.am.songoku at gmail.com
Wed Feb 26 18:33:53 CET 2014
Hi Bob,
>>>
In [3]: print metadata["artist"]
[u'The Incredible String Band']
<<<
Here u' and ' is not something you can strip off as it is part of python
datatype called UNICODE. Python prints a word or sentence in double or
singles quotes when it is a STRING or UNICODE in interpreter. These are
python datatypes. And even if there are any whitespaces in your data,
For example: a = [u' The Incredible String Band ']
Here there are leading and trailing spaces in the string which is inside
the LIST. Do a
a[0].strip()
Another thing above is that your string in inside a LIST so to access the
string to strip it, you need to specify the place value as i have done
above.
If you do the above you can get the string as you need it.
And if you dont need a 'u' in front of your string simply convert it to a
string with str() method like below.
>>> s = u'spam'
>>>
>>>
>>> s
u'spam'
>>> type(s)
<type 'unicode'>
>>> str(s)
'spam'
>>> type(str(s))
<type 'str'>
>>>
Regards,
Krishnan
On Wed, Feb 26, 2014 at 10:39 PM, Bob Williams
<linux at barrowhillfarm.org.uk>wrote:
> -----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-----
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140226/417f6fc9/attachment-0001.html>
More information about the Tutor
mailing list