fixing UnicodeEncodeError: 'charmap' codec can't encode character in python3
<p>I have a file input.txt which contains only one line: "obra鑾n". How I got that kanji there is <a href="https://stackoverflow.com/questions/36196168/recognising-encodings-in-excel-and-emacs">a separate problem</a> which I do not wish to address here, here I'd just like to print content of that file on command line using Python 3 on Windows (I do not have this issue on Linux).</p>

<p>I've been googling this issue for an hour now and have lost my mind without figuring out the solution. Here is how far I got:</p>

<pre><code># -*- coding: utf-8 -*-
f = open("input.txt", encoding='utf8')
s = f.read()

print(type(s))
#print(s) #error
b = s.encode('utf-8')
print(type(b))
print(b)
#print(b.decode("utf-8")) #error
#print(b.decode('unicode_escape')) #error
</code></pre>

<p>The output of this code is:</p>

<pre><code>&lt;class 'str'&gt;
&lt;class 'bytes'&gt;
b'obra\xe9\x91\xben\n'
</code></pre>

<p>Error on first two commented lines is identical:</p>

<pre><code>UnicodeEncodeError: 'charmap' codec can't encode character '\u947e' in position 4: character maps to &lt;undefined&gt;
</code></pre>

<p>Error on the last commented line is:</p>

<pre><code>UnicodeEncodeError: 'charmap' codec can't encode character in position 5-6: character maps to &lt;undefined&gt;
</code></pre>

<p>I have now run our of ideas what to do. Any ideas on how to print the content of this file on Windows command line using Python 3?</p>

<p>Thanks.</p>