0

I installed 3.5.1 Python several months ago on Windows 7. I have a python program which is in .py file. It runs fine from IDLE Python.

However, when I run it from command prompt, it says:

C:\_1\Python>info_.py
START -------------------------
Traceback (most recent call last):
  File "C:\_1\Python\info_.py", line 33, in <module>
    print (data)
  File "C:\Program Files\Python35\lib\encodings\cp866.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xab' in position 18
4: character maps to <undefined>

data is:

u = urllib.request.urlopen('http://www.****')
data = u.read()

I want to run programs from prompt too. What could I do to be sure my code works same in IDLE and via command prompt on Windows?

Alex Martian
  • 3,423
  • 7
  • 36
  • 71
  • 1
    your problem is similar to one stated here http://stackoverflow.com/questions/27092833/unicodeencodeerror-charmap-codec-cant-encode-characters – Harwee May 23 '16 at 11:37

1 Answers1

1

Try using encode

print(data.encode('utf-8'))
Harwee
  • 1,601
  • 2
  • 21
  • 35
  • 1
    This is just printing the repr of a `bytes` object, so `"«"` will print as `b'\xc2\xab'`. Printing Unicode in the Windows console requires a separate wide-character API that Python doesn't use by default. Install [`win_unicode_console`](https://pypi.python.org/pypi/win_unicode_console) to enable this API. – Eryk Sun May 23 '16 at 20:53