Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1741809
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2019-03-20 19:48:26

今天发现了一个奇怪的现象,直接上代码


点击(此处)折叠或打开

  1. import sys
  2. from colorama import Fore, Back, Style

  3. def print_example():
  4.     print(Fore.RED+ "1. This is the first sentence")
  5.      long_str=' Blowing in the wind'
  6.     print(Fore.WHITE + '2. long_str =', end='')
  7.     #print('2. long_str= ', end='')
  8.     print( long_str,file=sys.stderr)
  9.     print(Fore.CYAN + "3. Do they printed in right order")

  10. print_example()

  11. 打印出来的结果并不是我设想的顺序

  12. 1. This is the first sentence
  13.  Blowing in the wind
  14. 2. long_str =3. Do they printed in right order

为什么stderr和stdout会打印到一起呢? 我的猜想也许跟stdout, stderr的缓冲机制有关 后来发现python 3.3 后print可以加flush参数, 这样就能得到我想要的打印次序


点击(此处)折叠或打开

  1. import sys
  2.     from colorama import Fore, Back, Style
  3.     def print_example():
  4.         print(Fore.RED+ "1. This is the first sentence")
  5.          long_str=' Blowing in the wind'
  6.         print(Fore.WHITE + '2. long_str =', end='',flush=True)
  7.         #print('2. long_str= ', end='')
  8.         print( long_str,file=sys.stderr)
  9.         print(Fore.CYAN + "3. Do they printed in right order")
  10.     print_example()
  11.     打印出来如下
  12.     1. This is the first sentence
  13.     2. long_str = Blowing in the wind
  14.     3. Do they printed in right order

最后附上参考资料如何使用python打印彩色

阅读(3034) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~