简体   繁体   English

将文本颜色保存到python中的文本文件?

[英]Saving text color to a text file in python?

I am wondering, is there any way to save the color of text when printing stdout to a text file? 我想知道,在将标准输出打印到文本文件时,有什么方法可以保存文本的颜色? Here is a snippet of code for example: 例如,这是一段代码:

import sys, time, shutil

sys.stdout = open('testytest.txt', 'w')
print "\033[0;32mSome text"

time.sleep(1)

#"Some text" should still be colored when reprinted
with open('testytest.txt', 'r') as testfile:
     shutil.copyfileobj(testfile, sys.stdout)

So, once again, is there any way to save color when stdout is being saved to a text file? 因此,再次将stdout保存到文本文件时,有什么方法可以保存颜色? Or is it already saved? 还是已经保存?

Well you print the escape characters as well. 好了,您也可以打印转义字符。

So if you save the data that is printed to the stdout like: 因此,如果您保存打印到标准输出的数据,例如:

python somescript.py > somefile

That file will contain the escape chars as well. 该文件还将包含转义字符。

This to conclude: it is already saved . 可以得出结论: 它已经保存了 Instructions to modify the color actually don't exist. 修改颜色的说明实际上并不存在。 The program simply writes a binary stream and some shell visual programs interpret this as command to color. 该程序仅编写二进制流,而一些外壳程序可视程序将其解释为着色命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM