<div dir="ltr">Hi Bob,<div><br></div><div>>>></div><div><span style="font-family:arial,sans-serif;font-size:13px">In [3]: print metadata["artist"]</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">[u'The Incredible String Band']</span><br></div><div><font face="arial, sans-serif"><<<</font></div><div><font face="arial, sans-serif"><br></font></div>
<div style><font face="arial, sans-serif">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,</font></div>
<div style><font face="arial, sans-serif"><br></font></div><div style><font face="arial, sans-serif">For example:  a = </font><span style="font-family:arial,sans-serif;font-size:13px">[u'   The Incredible String Band   ']</span></div>
<div style><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div style><span style="font-family:arial,sans-serif;font-size:13px">Here there are leading and trailing spaces in the string which is inside the LIST. Do a </span></div>
<div style><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div style><span style="font-family:arial,sans-serif;font-size:13px">a[0].strip()</span></div><div style><span style="font-family:arial,sans-serif;font-size:13px"><br>
</span></div><div style><span style="font-family:arial,sans-serif;font-size:13px">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.</span></div>
<div style><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div style><font face="arial, sans-serif">If you do the above you can get the string as you need it.</font></div><div style><font face="arial, sans-serif"><br>
</font></div><div style><font face="arial, sans-serif">And if you dont need a 'u' in front of your string simply convert it to a string with str() method like below.</font></div><div style><font face="arial, sans-serif"><br>
</font></div><div style><font face="arial, sans-serif"><div>>>> s = u'spam'</div><div>>>> </div><div>>>> </div><div>>>> s</div><div>u'spam'</div><div>>>> type(s)</div>
<div><type 'unicode'></div><div>>>> str(s)</div><div style>'spam'</div><div>>>> type(str(s))</div><div><type 'str'></div><div>>>> </div></font></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br>
</span></div><div style><span style="font-family:arial,sans-serif;font-size:13px">Regards,</span></div><div style><span style="font-family:arial,sans-serif;font-size:13px">Krishnan</span></div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Wed, Feb 26, 2014 at 10:39 PM, Bob Williams <span dir="ltr"><<a href="mailto:linux@barrowhillfarm.org.uk" target="_blank">linux@barrowhillfarm.org.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-----BEGIN PGP SIGNED MESSAGE-----<br>
Hash: SHA1<br>
<br>
Hi List,<br>
<br>
I have two problems, but it's possible that one solution will suffice.<br>
I am using a module called mutagen to extract audio metadata from<br>
.flac files. The output of mutagen is in the form of a dictionary, so<br>
<br>
In [1]: import mutagen.flac<br>
<br>
In [2]: metadata = mutagen.flac.Open("/home/bob/music/artists/The<br>
Incredible String Band/1967 The 5000 Spirits Or The Layers Of The<br>
Onion/08 The Hedgehog's Song.flac")<br>
<br>
In [3]: print metadata["artist"]<br>
[u'The Incredible String Band']<br>
<br>
I now want to pass that string to another program, but I want to strip<br>
off the leading [u' and the trailing ']. However, this doesn't work:<br>
<br>
In [4]: artistName = metadata["artist"][3:-2]<br>
<br>
In [5]: print artistName<br>
[]<br>
<br>
I was expecting The Incredible String Band, not []<br>
<br>
What am I doing wrong? Or what have I misunderstood?<br>
<br>
The other problem concerns the program that receives these arguments -<br>
it complains (and stops with an error) if one the arguments is empty.<br>
For example, the flac file may not have the date information:<br>
<br>
Traceback (most recent call last):<br>
  File "/home/bob/Documents/scripts/python/flac2mp3v2.py", line 81, in<br>
<module> subprocess.call(['lame', '--add-id3v2',<br>
'--ignore-tag-errors', '--tt', str(metadata['title']), '--ta',<br>
str(metadata['artist']), '--tl', str(metadata['album']), '--ty',<br>
str(metadata['date']), '--tn', str(metadata['tracknumber']), '--tg',<br>
str(metadata['genre']), tempName1, tempName3])<br>
  File "/usr/lib/python2.7/site-packages/mutagen/__init__.py", line<br>
85, in __getitem__<br>
    else: return self.tags[key]<br>
  File "/usr/lib/python2.7/site-packages/mutagen/_vorbis.py", line<br>
184, in __getitem__<br>
    if not values: raise KeyError, key<br>
KeyError: 'date'<br>
<br>
If it's possible to edit the string value that gets passed to<br>
subprocess.call('lame'...) - see problem #1 above, would it also be<br>
possible to define a default value if the original field is empty?<br>
<br>
Bob<br>
- --<br>
Bob Williams<br>
System:  Linux 3.11.10-7-desktop<br>
Distro:  openSUSE 13.1 (x86_64) with KDE Development Platform: 4.12.2<br>
Uptime:  12:00pm up 13 days 20:00, 6 users, load average: 0.10, 0.19, 0.26<br>
-----BEGIN PGP SIGNATURE-----<br>
Version: GnuPG v2.0.22 (GNU/Linux)<br>
Comment: Using GnuPG with Thunderbird - <a href="http://www.enigmail.net/" target="_blank">http://www.enigmail.net/</a><br>
<br>
iEYEARECAAYFAlMOH9sACgkQ0Sr7eZJrmU5ufACeILRlmiXt4CgDa6ZpdTI3Npm5<br>
FToAn2+AcjNKGxJKU+9nE9IdsoEqlQdd<br>
=JpdC<br>
-----END PGP SIGNATURE-----<br>
_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="https://mail.python.org/mailman/listinfo/tutor" target="_blank">https://mail.python.org/mailman/listinfo/tutor</a><br>
</blockquote></div><br></div>