Unicode Error when opening text file - Geany
<p>I'm trying to create a little program that reads the contents of two stories, Alice in Wonderland &amp; Moby Dick, and then counts how many times the word 'the' is found in each story.</p>

<p>However I'm having issues with getting Geany text editor to open the files. I've been creating and using my own small text files with no issues so far.</p>

<pre><code>with open('alice_test.txt') as a_file:
    contents = a_file.readlines()

print(contents)
</code></pre>

<p>I get the following error:</p>

<pre><code>Traceback (most recent call last):
  File "add_cats_dogs.py", line 50, in &lt;module&gt;
    print(contents)
  File "C:\Users\USER\AppData\Local\Programs\Python\Python35-32\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2018' in position 279: character maps to &lt;undefined&gt;
</code></pre>

<p>As I said, no issues experienced with any small homemade text files.</p>

<p>Strangely enough, when I excecute the above code in Python IDLE, I have no problems, even if I change the text file's encoding between UTF-8 to ANSII.</p>

<p>I tried encoding the text file as UTF-8 and ANSII, I also checked to make sure the default encoding of Geany is UTF-8 (also tried without using default encoding), as well using and not using fixed encoding when opening non-Unicode files. </p>

<p>I get the same error every time. The text file was from gutenberg.org, I tried using another file from there and got the same issue.</p>

<p>I know it must be some sort of issue between Geany and the text file, but I can't figure out what.</p>

<p>EDIT: I found a sort of fix. 
Here is the text that was giving me problems:<a href="https://www.gutenberg.org/files/11/11-0.txt" rel="nofollow">https://www.gutenberg.org/files/11/11-0.txt</a>
Here is the text that I can use without problems:<a href="http://www.textfiles.com/etext/FICTION/alice13a.txt" rel="nofollow">http://www.textfiles.com/etext/FICTION/alice13a.txt</a>
Top one is encoded in UTF-8, bottom one is encoded in windows-1252. I would've imagined the reverse to be true, but for whatever reason the UTF-8 encoding seems to be causing the problem.</p>

<p>What OS do you use? There are similar problems in Windows. If so, you can try to run <code>chcp 65001</code> before you command in console. Also you can add <code># encoding: utf-8</code> at the top of you <code>.py</code> file. Hope this will help because I can't reply same encoding problem with .txt file from gutenberg.org on my machine.</p>

python python-3.x encoding text-files geany