Why does termcolor output control characters instead of colored text in the Windows console?

前端 未结 5 1459
北恋
北恋 2020-11-28 12:12

I just installed termcolor for Python 2.7 on Windows. When I try to print colored text, I get the color codes instead.

from termcolor import colored
print co         


        
5条回答
  •  日久生厌
    2020-11-28 12:45

    To make the ANSI colors used in termcolor work with the windows terminal, you'll need to also import/init colorama;

    >>> from termcolor import *
    >>> cprint('hello', 'red')
    ←[31mhello←[0m
    >>> import colorama
    >>> colorama.init()
    >>> cprint('hello', 'red')
    hello                                    <-- in red color
    >>>
    

提交回复
热议问题