Sublime Forum

Windows ST3: Python 3 unicode output

#1

I’m unable to run a script with unicode output with the default Python build system. Some unicode characters seem to get passed over, and others result in error.

print("é") > No output

print("⌂") > UnicodeEncodeError: ‘charmap’ codec can’t encode character ‘\u2302’ in position 0: character maps to

The output is fine in the command line but I want them to work from ST3 in the output panel. I noticed the Python interpreter in the ST3 console can run these lines fine. I tried to apply the information from https://forum.sublimetext.com/t/solved-build-results-unicode-output/6328/3, but I don’t know what else to try except

[code]import sys
import codecs
import imp

imp.reload(sys)
enc = “utf-8” # or “cp1252”
sys.stdout = codecs.getwriter(enc)(sys.stdout, ‘replace’)
print(“é”)[/code]

I get this error message on the last line: TypeError: must be str, not bytes

In an empty program, print(sys.stdout) results in <_io.TextIOWrapper name=’’ mode=‘w’ encoding=‘cp1252’>

I don’t know if other programming languages are unable to output unicode.

0 Likes

#2

as far as I know to use Unicode characters in python scripts(in the file itself) you need to add a header in the first line of the script

coding=utf8

0 Likes

#3

This is wrong, Python uses utf8 for source files by default (in all relevant recent versions). The issue is that the command prompt that gets executed in the back is encoded in your system codepage (most likely cp1252).

Quick fix: Open Python/Python.sublime-build using PackageResourceViewer (install via Package Control) and add this line:

"encoding": "<encoding>",

where “” is the same as this line exeucted in the console gives you:

import locale; locale.getpreferredencoding()

See github.com/SublimeTextIssues/De … issues/110.

0 Likes

#4

SOLVED (?)

I solved this for myself back in the summer, but I did not follow up here with sharing my solution. I came across the issue again recently, and checked my previous project. See the documentation for io.TextIOWrapper at https://docs.python.org/3/library/io.html#io.TextIOWrapper. The following code build output properly displays in the output panel as desired:

import sys from io import TextIOWrapper sys.stdout = TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') print("é") print("⌂") print("�")

@FichteFoll I’m not at all familiar with PackageResourceViewer or Package Control to implement your suggested quick fix, but adding the line directly to the Python.sublime-build file displayed é, but not ⌂. Interestingly, combining the solutions breaks the output, showing é instead of é, and ⌂ instead of ⌂.

There is a drawback that the above code block does not properly run in the Python IDLE. There may be more to discover here, but this solution satisfies enough for now.

0 Likes