我试图从error变量中去除“错误:”这个词,我想去除错误:对于所有情况?我该怎么做?
error = "ERROR:device not found"
#error = "error:device not found"
#error = "Error:device not found"
print error.strip('error:')
Run Code Online (Sandbox Code Playgroud)
该re模块可能是您最好的选择:
re.sub('^error:', '', 'ERROR: device not found', flags=re.IGNORECASE)
# '^error:' means the start of the string is error:
# '' means replace it with nothing
# 'ERROR: device not found' is your error string
# flags=re.IGNORECASE means this is a case insensitive search.
# In your case it would probably be this:
re.sub('^error:', '', error, flags=re.IGNORECASE)
Run Code Online (Sandbox Code Playgroud)
这将删除ERROR:字符串开头的所有变体。