title: De-mojibaking with Python and mutagen
body: <p>I'm reading mojibaked ID3 tags with <code>mutagen</code>. My goal is to fix the mojibake while learning about encodings and Python's handling thereof. </p>
  
  <p>The file I'm working with has an <code>ID3v2</code> tag, and I'm looking at its album (<code>TALB</code>) frame, which is, according to the encoding byte in the <code>TALB</code> ID3 frame, encoded in Latin-1 (<code>ISO-8859-1</code>). I know that the bytes in this frame, however, are encoded in <code>cp1251</code> (Cyrillic). </p>
  
  <p>Here's my code so far:</p>
  
  <pre><code> &gt;&gt;&gt; from mutagen.mp3 import MP3
   &gt;&gt;&gt; mp3 = MP3(paths[0])
   &gt;&gt;&gt; mp3['TALB']
   TALB(encoding=0, text=[u'\xc1\xf3\xf0\xe6\xf3\xe9\xf1\xea\xe8\xe5 \xef\xeb\xff\xf1\xea\xe8'])
  </code></pre>
  
  <p>Now, as you can see, <code>mp3['TALB'].text[0]</code> is represented here as a Unicode string. However, it's mojibaked:</p>
  
  <pre><code> &gt;&gt;&gt; print mp3['TALB'].text[0]
   Áóðæóéñêèå ïëÿñêè
  </code></pre>
  
  <p>I am having very little luck at transcoding these <code>cp1251</code> bytes into their correct Unicode codepoints. My best results so far have been very unbecoming:</p>
  
  <pre><code>&gt;&gt;&gt; st = ''.join([chr(ord(x)) for x in mp3['TALB'].text[0]]); st
  '\xc1\xf3\xf0\xe6\xf3\xe9\xf1\xea\xe8\xe5 \xef\xeb\xff\xf1\xea\xe8'
  &gt;&gt;&gt; print st.decode('cp1251')
  Буржуйские пляски &lt;-- **this is the correct, demojibaked text!**
  </code></pre>
  
  <p>As I understand this approach, it works because I end up transforming the Unicode string into an 8-bit string, which I can then decode into Unicode, while specifying the encoding I am decoding from.</p>
  
  <p>The problem is that I can't <code>decode('cp1251')</code> on the Unicode string directly:</p>
  
  <pre><code>&gt;&gt;&gt; st = mp3['TALB'].text[0]; st
  u'\xc1\xf3\xf0\xe6\xf3\xe9\xf1\xea\xe8\xe5 \xef\xeb\xff\xf1\xea\xe8'
  &gt;&gt;&gt; print st.decode('cp1251')
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
    File "/Users/dmitry/dev/mp3_tag_encode_convert/lib/python2.7/encodings/cp1251.py", line 15, in decode
      return codecs.charmap_decode(input,errors,decoding_table)
  UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128)
  </code></pre>
  
  <p>Can someone explain this? I can't understand how to make it not decode into the 7-bit <code>ascii</code> range when operating directly on the <code>u''</code> string. </p>