pythonpython-3.xcmdcharacter-encodinghebrew

When using CMD terminal Python can't display Hebrew


I'm trying to use Hebrew Characters in my code. but my terminal (CMD) can't support Hebrew and instead it just displays random question marks. For example:

>>>Print("דוגמא")

result: ????? \

EDIT: The CMD console simply has no Hebrew letters in its default font. You can simply change the font to display Hebrew.


Solution

  • I have a solution. Change console font in Windows There is a code there to change the font. I found out it also works for fonts that are usually not supported by CMD.

    according to the answer:

    import ctypes
    
    LF_FACESIZE = 32
    STD_OUTPUT_HANDLE = -11
    
    class COORD(ctypes.Structure):
        _fields_ = [("X", ctypes.c_short), ("Y", ctypes.c_short)]
    
    class CONSOLE_FONT_INFOEX(ctypes.Structure):
        _fields_ = [("cbSize", ctypes.c_ulong),
                    ("nFont", ctypes.c_ulong),
                    ("dwFontSize", COORD),
                    ("FontFamily", ctypes.c_uint),
                    ("FontWeight", ctypes.c_uint),
                    ("FaceName", ctypes.c_wchar * LF_FACESIZE)]
    
    font = CONSOLE_FONT_INFOEX()
    font.cbSize = ctypes.sizeof(CONSOLE_FONT_INFOEX)
    font.nFont = 12
    font.dwFontSize.X = 11
    font.dwFontSize.Y = 18
    font.FontFamily = 54
    font.FontWeight = 400
    font.FaceName = "Lucida Console"
    
    handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
    ctypes.windll.kernel32.SetCurrentConsoleFontEx(
            handle, ctypes.c_long(False), ctypes.pointer(font))```