1

I can't find solution for error:

UnicodeEncodeError: 'charmap' codec can't encode character '\x96' in position 582: character maps to <undefined>

which appears when I try to redirect output to file with:

python.exe page_query.py > output.html

There is no problem to display output in powershell with just:

python.exe page_query.py

but I had to use chcp 65001 command first.

Here is my short code:

import requests

payload = {'st': 'C3225X6S0J107M250AC'}
r = requests.get('http://pl.farnell.com/webapp/wcs/stores/servlet/Search?catalogId=15001&langId=-22&storeId=10170&categoryName=Wszystkie%20kategorie&selectedCategoryId=&gs=true&', params=payload)
print(r.encoding)
unicode_str = r.text
print(unicode_str)

Could you help with it?

bLAZ
  • 1,485
  • 3
  • 16
  • 28
  • Possible duplicate of [UnicodeEncodeError: 'charmap' codec can't encode - character maps to , print function](http://stackoverflow.com/questions/14630288/unicodeencodeerror-charmap-codec-cant-encode-character-maps-to-undefined) – Ilja Everilä Sep 21 '16 at 07:15
  • See also: http://stackoverflow.com/a/32176732/6845813 – Teemu Risikko Sep 21 '16 at 07:17
  • @IljaEverilä with mentioned answers I solved problem with powershell output, but I don't know how to solve it when it is redirected to file. As I understand it is the problem with the console encoder. So why it doesn't work when it is not printed in console...? – bLAZ Sep 21 '16 at 07:54
  • 1
    [This comment](http://stackoverflow.com/questions/14630288/unicodeencodeerror-charmap-codec-cant-encode-character-maps-to-undefined#comment52718147_16120218) seems to hint that non interactive streams (pipes, redirects) default to codepage encoding handling, and so when you're trying to output unicode with codepoints that cannot be represented in to a file, it breaks. See the linked Q/As for ways to handle output encoding in that case. – Ilja Everilä Sep 21 '16 at 08:14
  • 2
    You need to open the file and specify the `encoding="utf-8"` – Padraic Cunningham Sep 21 '16 at 10:50
  • 1
    @PadraicCunningham writing to file in the code helped me. Thanks a lot! – bLAZ Sep 21 '16 at 11:01

0 Answers0