Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将终端输出重定向到文件,同时保留颜色_Python_Rendering - Fatal编程技术网

Python 将终端输出重定向到文件,同时保留颜色

Python 将终端输出重定向到文件,同时保留颜色,python,rendering,Python,Rendering,我有以下代码从路径列表(以字符串格式)呈现树结构: 它允许根据flag属性为节点着色。例如: paths = ["x1/x2", "x1/x2/x3", "x1/x4", "x1/x5/x6"] root = TreeNode("x1", None) for path in paths: find_and_insert(root, path.split("/")[1:

我有以下代码从路径列表(以字符串格式)呈现树结构:

它允许根据flag属性为节点着色。例如:

paths = ["x1/x2", "x1/x2/x3", "x1/x4", "x1/x5/x6"]
root = TreeNode("x1", None)

for path in paths:
    find_and_insert(root, path.split("/")[1:])

root.print(True)
生成以下输出:

x1  (red)
├───x2  (green)
│   └───x3 (red)
├───x4 (red)
└───x5 (green)
    └───x6 (red)
(注意端子输出中显示的颜色,但此处不显示。)

我的问题是:我想在保存颜色的同时,在.txt或.doc文件中保存一个树结构(对于更大的树)。将输出复制并粘贴到.doc/.docx文件中是可行的,但我需要对几个树执行此操作,因此我想找到一种pythonic方法来执行此操作。有什么想法吗?提前谢谢

x1  (red)
├───x2  (green)
│   └───x3 (red)
├───x4 (red)
└───x5 (green)
    └───x6 (red)