frank hk
frank hk

Reputation: 127

UnicodeEncodeError: 'charmap' codec can't encode character '\u2264'

Detailed Error Log:

Traceback (most recent call last):
  File "C:\Dev\EXE\TEMP\cookie\crumbs\views.py", line 1520, in parser
    html_file.write(html_text)
  File "C:\Users\Cookie1\AppData\Local\Programs\Python\Python36-32\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2264' in position 389078: character maps to <undefined>

Upvotes: 1

Views: 1950

Answers (1)

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 48962

The error message indicates that you're trying to encode to the Windows-1252 character encoding. That encoding doesn't have a representation of the less than or equal symbol.

>>> "\u2264".encode("cp1252")
>>> Traceback... [as above]

The answer is to use UTF-8, an unrestricted encoding, instead of Windows-1252, a very restricted encoding.

Your question doesn't include much context, but the line html_file.write(html_text) makes me think you're using Python's file protocol. The documentation for open() shows how to set the encoding, e.g.

html_file = open("file.html", mode="w", encoding="utf8")

Note that "the default encoding is platform dependent (whatever locale.getpreferredencoding() returns)", which is why you're getting Windows-1252 on Windows 7.

Upvotes: 6

Related Questions