-2

I want to print an ASCII text but when I run the script, it throws me an error:

$ python test.py Traceback (most recent call last): 
   File "C:\Users\wooxh\Desktop\Materialy\XRichPresence\test.py", 
       line 1, in <module> print(""" File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\encodings\cp1250.py", 
           line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 
   'charmap' codec can't encode characters in position 2-4: character maps to <undefined>

Here's the code

print("""
██╗  ██╗██████╗ ██████╗  ██████╗
╚██╗██╔╝██╔══██╗██╔══██╗██╔════╝
 ╚███╔╝ ██████╔╝██████╔╝██║     
 ██╔██╗ ██╔══██╗██╔═══╝ ██║     
██╔╝ ██╗██║  ██║██║     ╚██████╗
╚═╝  ╚═╝╚═╝  ╚═╝╚═╝      ╚═════╝
""")
Jab
  • 26,853
  • 21
  • 75
  • 114
  • 1
    Remove the `encode` call. – luk2302 Nov 02 '21 at 15:57
  • It's printing encoded because you are encoding it... – Jab Nov 02 '21 at 15:58
  • Seems to work if you remove the `.encode("UTF-8")` at the end. – zaxishere Nov 02 '21 at 15:58
  • Why are you encoding it? `print` doesn't handle `bytes` (it just calls `str()` on them which ends up printing their `repr`), so if you need to write raw bytes in a specific encoding, you'll have to use `sys.stdout.buffer.write()`, but I'm unclear why you don't just `print` the string without encoding and let `print` (really `sys.stdout`) encode from text to bytes for you. – ShadowRanger Nov 02 '21 at 15:58
  • Voting to close, as caused by typo *(kinda)* – Jab Nov 02 '21 at 15:59
  • I Added the .encode call because it would throw me another error – WooxHimself Nov 02 '21 at 15:59
  • @WooxHimself then update your post to highlight that error; you shouldn't arbitrarily add things to code. – Hazel へいぜる Nov 02 '21 at 15:59
  • 3
    @WooxHimself Then why did you not include the traceback? – Jab Nov 02 '21 at 15:59
  • 1
    @WooxHimself: Odds are that's because your terminal doesn't support UTF-8 (or at least, Python doesn't think it does). That's a configuration problem slightly outside of Python. – ShadowRanger Nov 02 '21 at 16:00
  • @WooxHimself: What version of Python do you have installed? Python 3.7 [supports a forced UTF8 runtime mode](https://docs.python.org/3/whatsnew/3.7.html#pep-540-forced-utf-8-runtime-mode) that might be enough to make this code work (Windows' `cmd.exe` runs in a non-UTF8 mode by default). – ShadowRanger Nov 02 '21 at 16:02
  • I edited the question to include the traceback OP posted on Taco's answer. – Jab Nov 02 '21 at 16:05
  • @WooxHimself: Assuming you're running in `cmd.exe` on Windows, can you run `chcp` in the `cmd.exe` terminal (not in Python) and report what it displays? For me, it's `Active code page: 437`, which means the `print` *should* work (or `sys.stdout.buffer.write('''the string'''.encode('cp437'))` to explicitly encode); I'm not 100% on how potentially UTF-8 coercion with a terminal using cp437 might interact. – ShadowRanger Nov 02 '21 at 16:06
  • Does this answer your question? [UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to ](https://stackoverflow.com/questions/9233027/unicodedecodeerror-charmap-codec-cant-decode-byte-x-in-position-y-character) – rustyhu Nov 02 '21 at 16:07

1 Answers1

1

Looks like Python is identifying your code page as 1250, which doesn't include the characters you're using. If chcp reports you're actually using code page 437 (common in cmd.exe) you can do:

import sys

sys.stdout.buffer.write("""
██╗  ██╗██████╗ ██████╗  ██████╗
╚██╗██╔╝██╔══██╗██╔══██╗██╔════╝
 ╚███╔╝ ██████╔╝██████╔╝██║     
 ██╔██╗ ██╔══██╗██╔═══╝ ██║     
██╔╝ ██╗██║  ██║██║     ╚██████╗
╚═╝  ╚═╝╚═╝  ╚═╝╚═╝      ╚═════╝
""".encode('cp437'))

to explicitly encode to the correct code page and write it. Otherwise, I'd suggest enabling Python's forced UTF-8 runtime mode, which should allow your original code (with no call to encode) to work (possibly dropping or replacing characters not representable by the terminal). All you'd change is your run command:

> python -X utf8 test.py

or explicitly define PYTHONUTF=1 in your environment to turn it on without a command line switch.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271