Talk:O'Reilly Introduction to Python/Section 13

From OpenHatch wiki
Revision as of 12:41, 4 February 2017 by (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This error came up after my first entry in Section 14 "Flash Card Quizzer". The french_food.txt file has characters which are not recognized. Will have to research on changing the script to support.

Here is the "Traceback", pretty sure I can fix.

Question: le repas Your guess: meal Correct! Traceback (most recent call last):

 File "", line 33, in <module>
   print("Question: " + question)
 File "C:\Python34\lib\encodings\", line 19, in encode
   return codecs.charmap_encode(input,self.errors,encoding_map)[0]

UnicodeEncodeError: 'charmap' codec can't encode characters in position 14-15: character maps to <undefined>

I also had the same problem --- it was due to some french letters (else the code was running good with alphanumeric text)

Trick: for Python 3 in Windows 10

If you're running code ("" file) from command prompt (PowerShell(x86)) terminal, then

1) To check:


2) To set utf-8:

      chcp 65001         (<<<< sets the encoding to UTF-8 in Windows)

3) Now make below changes in the file "french_food.txt"

      use any method:
      ## Method 1 >>> f = open(flashcard_filename, "r", encoding="utf-8")     
      ## Method 2 >>> f = open(flashcard_filename, "r", encoding="cp65001")

Now run your "" script from PowerShell(x86)

      py -3.4 french_food.txt

5) Back to default:

      chcp 437

[helloworld024 @ stackoverflow]