Talk:O'Reilly Introduction to Python/Section 13

From OpenHatch wiki
Jump to navigation Jump to search

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 "flashcards.py", line 33, in <module>
   print("Question: " + question)
 File "C:\Python34\lib\encodings\cp437.py", 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 ("flashcards.py" file) from command prompt (PowerShell(x86)) terminal, then

1) To check:

      chcp

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 "flashcards.py" script from PowerShell(x86)

      py -3.4 flashcards.py french_food.txt

5) Back to default:

      chcp 437


[helloworld024 @ stackoverflow]