简体   繁体   English

在python中处理终端颜色代码(ANSI颜色转义代码)

[英]Handling terminal colour codes ( ANSI colour escape codes ) in python

在此处输入图片说明

I was supposed to write a PyQt application which can communicate with serial devices over UART. 我应该写一个可以通过UART与串行设备通信的PyQt应用程序。 I connected it to an OpenWRT based router and when I tried to run 'ls' on it, the output ( see attached screenshot ) had some weird letters around the name of the folders. 我将其连接到基于OpenWRT的路由器,当我尝试在其上运行'ls'时,输出(请参见随附的屏幕截图)在文件夹名称周围带有一些奇怪的字母。 I understand that these are Color codes of some sort. 我了解这些是某种颜色代码。 How do I remove these from the output or display the colour accordingly. 如何从输出中删除它们或相应显示颜色。 Any help would help. 任何帮助都会有所帮助。

See the man page for ls (1), and the output of the command 请参见手册页中的ls (1)和命令的输出

$  command -v ls

On my Linux system, that reports 在我的Linux系统上,该报告

$ command -v ls
alias ls='ls --color=auto'

It's a fact of life that a Unix interactive shell cannot reliably determine the characteristics of the attached terminal, and a regrettable fact that GNU colorization is indifferent to the terminal. Unix交互外壳无法可靠地确定所连接终端的特性是生活中的事实,而令人遗憾的事实是GNU着色对终端无动于衷。 When connecting with a device that doesn't want color, the best plan is just to turn it off wherever it crops up. 当与不需要颜色的设备连接时,最好的计划是在任何出现颜色的地方都将其关闭。

To test that, one quick trick is to type "ls" this way: 为了测试这一点,一个快速的技巧是通过这种方式键入“ ls”:

$ \ls

The introductory backslash defeats the shell's alias mechanism, and reverts to using bare /usr/bin/ls , or whatever your PATH supplies. 介绍性的反斜杠击败了shell的别名机制,并恢复为使用裸露的/usr/bin/lsPATH提供的任何东西。

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

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