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

Question:
What does this mean and how to fix it? It is raised upon running: file.write(str(data) + "\n")

Traceback (most recent call last):
  File "c:\Users\[REDACTED]\OneDrive\Documents\[REDACTED]\main.py", line 146, in <module>
    all()
  File "c:\Users\[REDACTED]\OneDrive\Documents\[REDACTED]\main.py", line 104, in all
    file.write(str(data) + "\n")
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.1776.0_x64__qbz5n2kfra8p0\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 '\ufb01' in position 10375854: character maps to <undefined>
PS C:\Users\[REDACTED]\OneDrive\Documents\[REDACTED]> 

Could you send the contents of the file you are reading from?

The file that I’m writing to? it shouldn’t matter. The problem is that for some reason it can’t encode \ufb01 (LATIN SMALL LIGATURE FI, also “fi”) for some reason. Maybe it’s because it is already encoded? Is \ufb01encoded? If so, how do I make it not encode it if it is already encoded?

Do you have the character in the string, or is it represented with Unicode in the string?

@boston2029 When I print out the string, it shows as “fi”, but the error states that it is trying to encode a already encoded string “/ufb01”, which would most likely be the cause of the error. Does casting to a string result in encoding characters to escape characters is possible? If so, does file.write() do the same?

Try changing it from to \ufb01 in your code, and vice-versa. Other than that idk

looks like something’s causing python to guess that the file is encoded using cp1252.
Add the , encoding="utf-8" argument to the open() call where you’re opening the file.


is the name of a built-in function. You’d rather not override it with something incompatible.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.