插入 ascii 转义字符时删除 python 中的双空格
Removing double whitespace in python when inserting ascii escape characters
如何在插入 ascii 转义字符的地方删除双白space。一切都按我的意愿工作,但唯一的问题是双白色 space 在我使用转义字符的地方。
class Print():
def __init__(self, type, content, bold=False, emphasis=False, underline=False, timestamp=True):
# Set color of the string
if type == "info":
self.start = "3[0m"
elif type == "error":
self.start = "3[91m"
elif type == "success":
self.start = "3[92m"
elif type == "warning":
self.start = "3[93m"
# Format style of the string
if bold:
self.start += "3[1m"
if emphasis:
self.start += "3[3m"
if underline:
self.start += "3[4m"
# Check for name and format it
string = content.split(" ")
formated_string = []
for word in string:
if word.startswith("["):
formated_string.append("3[96m")
formated_string.append(word)
if word.endswith("]"):
formated_string.append(self.start)
self.content = " ".join(formated_string)
# Set color and format to default values
self.end = "3[0m"
# Get current date and time
stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + ": "
print(f"{self.start}{stamp if timestamp == True else ''}{self.content}{self.end}")
当我将 Debug
模块导入我的代码时调用:
Debug.Print("info", "this is test [string] as example to [my] problem")
这是结果:
2021-11-03 20:16:09: this is test [string] as example to [my] problem
你可以注意到方括号前后的双白space。颜色格式不可见
>>> ' '.join([a for a in "this is test [string] as example to [my] problem".split(' ') if a])
'this is test [string] as example to [my] problem'
问题是,您将颜色值附加为额外元素,因此它会添加 2 个空格,因为颜色值是不可见的值,但也由空格连接。 (您可以打印 formated_string
以查看添加空格的所有值)。
您可以将代码更改为以下内容以修复它:
class Print():
def __init__(self, type, content, bold=False, emphasis=False, underline=False, timestamp=True):
# Set color of the string
if type == "info":
self.start = "3[0m"
elif type == "error":
self.start = "3[91m"
elif type == "success":
self.start = "3[92m"
elif type == "warning":
self.start = "3[93m"
# Format style of the string
if bold:
self.start += "3[1m"
if emphasis:
self.start += "3[3m"
if underline:
self.start += "3[4m"
self.end = "3[0m"
# Check for name and format it
string = content.split(" ")
formated_string = []
for word in string:
if word.startswith("["):
word = f"3[96m{word}"
if word.endswith("]"):
word = f"{word}{self.end}"
formated_string.append(word)
self.content = " ".join(formated_string)
# Set color and format to default values
self.end = "3[0m"
# Get current date and time
stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + ": "
print(f"{self.start}{stamp if timestamp == True else ''}{self.content}{self.end}")
如何在插入 ascii 转义字符的地方删除双白space。一切都按我的意愿工作,但唯一的问题是双白色 space 在我使用转义字符的地方。
class Print():
def __init__(self, type, content, bold=False, emphasis=False, underline=False, timestamp=True):
# Set color of the string
if type == "info":
self.start = "3[0m"
elif type == "error":
self.start = "3[91m"
elif type == "success":
self.start = "3[92m"
elif type == "warning":
self.start = "3[93m"
# Format style of the string
if bold:
self.start += "3[1m"
if emphasis:
self.start += "3[3m"
if underline:
self.start += "3[4m"
# Check for name and format it
string = content.split(" ")
formated_string = []
for word in string:
if word.startswith("["):
formated_string.append("3[96m")
formated_string.append(word)
if word.endswith("]"):
formated_string.append(self.start)
self.content = " ".join(formated_string)
# Set color and format to default values
self.end = "3[0m"
# Get current date and time
stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + ": "
print(f"{self.start}{stamp if timestamp == True else ''}{self.content}{self.end}")
当我将 Debug
模块导入我的代码时调用:
Debug.Print("info", "this is test [string] as example to [my] problem")
这是结果:
2021-11-03 20:16:09: this is test [string] as example to [my] problem
你可以注意到方括号前后的双白space。颜色格式不可见
>>> ' '.join([a for a in "this is test [string] as example to [my] problem".split(' ') if a])
'this is test [string] as example to [my] problem'
问题是,您将颜色值附加为额外元素,因此它会添加 2 个空格,因为颜色值是不可见的值,但也由空格连接。 (您可以打印 formated_string
以查看添加空格的所有值)。
您可以将代码更改为以下内容以修复它:
class Print():
def __init__(self, type, content, bold=False, emphasis=False, underline=False, timestamp=True):
# Set color of the string
if type == "info":
self.start = "3[0m"
elif type == "error":
self.start = "3[91m"
elif type == "success":
self.start = "3[92m"
elif type == "warning":
self.start = "3[93m"
# Format style of the string
if bold:
self.start += "3[1m"
if emphasis:
self.start += "3[3m"
if underline:
self.start += "3[4m"
self.end = "3[0m"
# Check for name and format it
string = content.split(" ")
formated_string = []
for word in string:
if word.startswith("["):
word = f"3[96m{word}"
if word.endswith("]"):
word = f"{word}{self.end}"
formated_string.append(word)
self.content = " ".join(formated_string)
# Set color and format to default values
self.end = "3[0m"
# Get current date and time
stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + ": "
print(f"{self.start}{stamp if timestamp == True else ''}{self.content}{self.end}")