如何使用python进行颜色输出?

时间:2016-06-28 08:57:04

标签: python python-2.7 unicode terminal

我是python的新手(约一周或更短时间),我正在使用python 2.7。

我正在编写一个程序来检查ip验证和类,我想以彩色输出,所以它在终端上对用户更具可读性。

我已经尝试过这个:

# to make the text purple for example.
print '\033[95m' + "This is a purple output"
# if i'm writing another simple print after the first one it will be purple too
print "Example" # now this statement purple too

但是当我使用这个例子时,第一个打印方法后的输出也变为紫色。

感谢所有尝试的人。

2 个答案:

答案 0 :(得分:2)

通过打印转义码\033[0m,您可以重置颜色:

>>> print '\033[95m' + "This is a purple output" + '\033[0m'
This is a purple output

或者,您可以使用colorama package

>>> from colorama import Fore
>>> print Fore.LIGHTMAGENTA_EX + "This is a purple output" + Fore.RESET
This is a purple output

答案 1 :(得分:1)

我认为这是我能想到的最佳方式。

@foreach ($images as $image )

    @if($count==0  OR is_int($count/3))
        <?php echo '<div class="row">';?>
     @endif

    <div class="col-md-3">
      <div class="well" style="background:white;">
        <img src="{{ url('images/'.$image->filepath) }}" alt="" class="" height="100" width="100" ></br>

       {{ $image->title }}<br>
        {{$image->filepath }}
      </div>
    </div>

    @if($count==0 OR is_int($count/3))
        <?php echo '</div>' ;?>

    @endif

    <?php $count++;?>
@endforeach

感谢@falsetru帮我解决这个问题。