Skip to content

UnicodeEncodeError: 'charmap' codec can't encode character exception on Windows #1773

Description

@nj1973

Example error

  File "c:\Users\x\dev\DVT\Lib\site-packages\data_validation\util.py", line 32, in timed_call
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "c:\Users\x\dev\DVT\Lib\site-packages\data_validation\result_handlers\text.py", line 75, in print_formatted_
    print(self._get_formatted(result_df))
  File "c:\Users\x\dev\DVT\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 '\x81' in position 351: character maps to <undefined>

Suggested workaround from Windows Command Prompt (CMD):

set PYTHONIOENCODING=utf-8
data-validation validate row -sc=c1 -tc=c2 --tbls ... > "%output_dir%\dbo_tblbank.csv"

Potential code change

To prevent crashes on Windows when printing non-ASCII / UTF-8 characters to redirected standard output, text.py can safely reconfigure standard output to UTF-8 before calling print :

import sys

# Ensure stdout uses UTF-8 encoding so redirected files don't fail on Windows cp1252
if hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8")

We should either document the workaround or implement the potential solution.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions