1

I'm learning Python and playing around with a Reddit bot, but I'm getting an error. I think I need to change the encoding of the PowerShell console window, but I'm not sure how. Here is the code:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import praw

user_agent = ("PyEng Bot 0.1")

r = praw.Reddit(user_agent = user_agent)

subreddit = r.get_subreddit("learnpython")

for submission in subreddit.get_hot(limit = 5):
    print "Title: ", submission.title
    print "Text: ", submission.selftext
    print "Score: ", submission.score
    print "---------------------------------\n"

And here is the error:

Text:  Traceback (most recent call last):
  File "redditbot.py", line 13, in <module>
    print "Text: ", submission.selftext
  File "C:\Python27\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2014' in  position 204: character maps to <undefined>
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
nbstrong
  • 115
  • 1
  • 10
  • I forgot to add, I am writing the script in Notepad++ and running it using PowerShell. – nbstrong Aug 23 '15 at 20:55
  • 1
    If you forgot to add something to your question, you should edit the information into your question instead of adding a comment. About your issue, I suspect it has nothing to do with PowerShell, so you should remove the PowerShell tag unless you can demonstrate how it does. If you run it from the Windows command prompt, does your issue disappear? – briantist Aug 23 '15 at 21:04
  • 1
    I suggest using Python 3.x, as it is more apparent where you need to encode and when encode errors happen. – Mikko Ohtamaa Aug 23 '15 at 21:18
  • 2
    to print Unicode characters that are not supported by the console encoding, you could try [`win-unicode-console` package or run the script in IDLE](http://stackoverflow.com/a/30551552/4279) – jfs Aug 24 '15 at 00:34
  • Problems with Unicode encoding and either of MS Windows' shells are pretty common. Please do some research: https://stackoverflow.com/search?q=python+unicode+error+windows – Ulrich Eckhardt Aug 24 '15 at 06:33
  • @UlrichEckhardt: most answers on the topic are either wrong or too complex. `win-unicode-console` started to support Python 2.7 only recently. It is worth to link to a solution that works directly. – jfs Aug 25 '15 at 08:15
  • Don't get me wrong, I think that your link makes a great answer, @J.F.Sebastian! However, this question looks like a duplicate to me. – Ulrich Eckhardt Aug 25 '15 at 18:13

0 Answers0