“Unicodeencodeerror: ‘charmap’ codec can’t encode characters in position” is a common error related to writing content to a file or reading content from a file. The below explanations can help you know more about the cause of this error and solutions.
How does the “unicodeencodeerror: ‘charmap’ codec can’t encode characters in position” error happen?
“Unicodeencodeerror: ‘charmap’ codec can’t encode characters in position” error is related to the file content. You will encounter this situation while making requests to write/read content that contains some characters that are not unicode encoded.
This error indicates that the encoding process could not be completed successfully. There may be because you have used a codec that is not able to encode the characters you are writing/reading. For example, the string below, when encoding to byte by the open()
function will cause the error:
myString = "café" f = open("demo", "a", encoding="cp875") f.write(myString) f.close()
Output:
UnicodeEncodeError: 'charmap' codec can't encode character '\xe9' in position 3: character maps to <undefined>
In general, the error occurs because your codec given doesn’t support encoding one or some characters in your strings into bytes that can be written or read to a file or print to screen. In some cases, the error message may be different, for instance:
myString = "café" f = open("demo", "a", encoding="euc-kr") f.write(myString) f.close()
Output:
UnicodeEncodeError: 'euc_kr' codec can't encode character '\xe9' in position 3: illegal multibyte sequence
How to solve the error?
Method 1: Use another type of codec to encode characters
You must specify the correct encoding (mostly utf-8) when encoding the string into bytes or reading/writing it from/to the file.
myString = "café" f = open("demo", "w", encoding="utf-8") f.write(myString) f = open("demo", "r", encoding="utf-8") print(f.read())
Output:
café
Method 2: Remove the incorrect char at the error position:
Another way to fix this problem is to delete the character that caused the error at the position in your error message. If you don’t want to delete the incorrect character, you can change it to some character in the Latin alphabet that doesn’t have any accent:
myString = "cafe" # (Removed incorrect char) instead of 'café' f = open("demo", "a", encoding = "euc-kr") f.write(myString) f.close() f = open("demo", "r") print(f.read())
Output:
cafe
Summary
The “unicodeencodeerror: ‘charmap’ codec can’t encode characters in position” error occurs when encountering a UTF-8 character which is not supported by the current encoding. When saving a request or response of url, it is always recommended to check for the UTF-8 codec so that the encoding can be propagated to avoid such a trivial problem, as it really wastes a lot of time in coding.
Maybe you are interested:
- ValueError: I/O operation on closed file in Python
- UnicodeDecodeError: ‘ascii’ codec can’t decode byte
- UnicodeDecodeError: ‘charmap’ codec can’t decode byte
- UnicodeEncodeError: ‘ascii’ codec can’t encode character in position

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R