title: How to read ® character from Windows-1252 file and write to UTF-8 file
body: <p>I have an input file in Windows-1252 encoding that contains the '®' character. I need to write this character to a UTF-8 file. Also assume I must use Python 2.7. Seems easy enough, but I keep getting UnicodeDecodeErrors.</p>
  
  <p>I originally had just opened the original file using <code>codecs.open()</code> with UTF-8 encoding, which worked fine for all of the ASCII characters until it encountered the ® symbol, whereupon it choked with the error:</p>
  
  <pre><code>UnicodeDecodeError: 'utf8' codec can't decode byte 0xae in position 2867043: 
  invalid start byte
  </code></pre>
  
  <p>I knew that I would have to properly decode it as cp1252 to fix this problem, so I opened it in the proper encoding and then encoded the data as UTF-8 prior to writing. But that produced a new error:</p>
  
  <pre><code>UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 22: 
  ordinal not in range(128)
  </code></pre>
  
  <p>Here is a minimum working example:</p>
  
  <pre><code>with codecs.open('in.txt', mode='rb', encoding='cp1252') as inf:
      with codecs.open('out.txt', mode='wb', encoding='utf-8') as of:
          for line in inf:
              of.write(line.encode('utf-8'))
  </code></pre>
  
  <p>Here is the contents of <code>in.txt</code>:</p>
  
  <pre><code>Sample file
  
  Here is my sample file® yay.
  </code></pre>
  
  <p>I thought perhaps I could just open it in 'rb' mode with no encoding specified and specifically handle the decoding and encoding of each line like so:</p>
  
  <pre><code>of.write(line.decode('cp1252').encode('utf-8'))
  </code></pre>
  
  <p>But that also didn't work, giving the same error as when I just opened it as UTF-8.</p>
  
  <p>How do I read data from a Windows-1252 file, properly decode it then encode it as UTF-8 and write it to a UTF-8 file? The above method has always worked for me in the past until I encountered the ® character.</p>