【发布时间】:2014-04-29 07:12:10
【问题描述】:
[1mResolving domain yahoo.com ,[0m ,98.138.253.109 yahoo.com
我想删除"[1m" 和"[0m"。我尝试使用encode('utf-8') 进行编码没有成功。
请帮忙。
【问题讨论】:
-
如何识别这些字符?
标签: python regex control-characters color-codes
[1mResolving domain yahoo.com ,[0m ,98.138.253.109 yahoo.com
我想删除"[1m" 和"[0m"。我尝试使用encode('utf-8') 进行编码没有成功。
请帮忙。
【问题讨论】:
标签: python regex control-characters color-codes
a="[1mResolving domain yahoo.com ,[0m ,98.138.253.109 yahoo.com"
c=["[1m","[0m"]
for i in c:
a=a.replace(i,"")
print a
如果你想删除更多,你可以将它添加到列表c
【讨论】:
显然,您正在从兼容 VT100 的终端接收颜色编码的字符串。字符串中应该有 #27 (0x1b, ESC) 符号,如果是这样,请使用正则表达式从字符串中消除以 ESC 符号开头的颜色代码。您可以查看可能的颜色代码列表in this Wikipedia article.
对不起,我不是编写正则表达式或 Python 代码的专家,所以请使用其他答案来编写代码。
【讨论】:
问题中的信息太少,但我认为应该这样做:
import re
s = '[1mResolving domain yahoo.com ,[0m ,98.138.253.109 yahoo.com'
t = re.sub('\[\dm', '', s)
【讨论】: