简体   繁体   中英

How to remove unnecessary characters from this kind of string?

[1mResolving domain yahoo.com ,[0m ,98.138.253.109  yahoo.com

I want to remove "[1m" and "[0m" . I tried encoding using encode('utf-8') didn't worked.

Please help.

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

if you want remove any more ,you can add it to list c

Apparently you're receiving a color-coded string from a VT100-compatible terminal. There should be #27 (0x1b, ESC) symbols in the string, if so, use the regexp eliminating color codes starting with ESC symbols from the string. You can check the possible list of color codes in this Wikipedia article.

Sorry I'm not an expert to write a regexp or Python code, so please use other answers for codes.

There's too little information in the question, but I think the following should do it:

import re
s = '[1mResolving domain yahoo.com ,[0m ,98.138.253.109    yahoo.com'
t = re.sub('\[\dm', '', s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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