0

I have one string that is color terminal text (bash), now I'd like to display it as plain text via removing the color, is there any way or library to do that? Here's one example color text:

[1m[34mpeopleSchema[0m: [1m[32morg.apache.spark.sql.types.StructType[0m = StructType(StructField(id,IntegerType,true), StructField(name,StringType,true))
[1m[34mdf[0m: [1m[32morg.apache.spark.sql.DataFrame[0m = [id: int, name: string]
[1m[34mres1[0m: [1m[32morg.apache.spark.sql.DataFrame[0m = []
zjffdu
  • 25,496
  • 45
  • 109
  • 159

1 Answers1

0

You can try to remove them with regular expressions, i.e.

import re

dirty = '''[1m[34mpeopleSchema[0m: [1m[32morg.apache.spark.sql.types.StructType[0m = StructType(StructField(id,IntegerType,true), StructField(name,StringType,true))
[1m[34mdf[0m: [1m[32morg.apache.spark.sql.DataFrame[0m = [id: int, name: string]
[1m[34mres1[0m: [1m[32morg.apache.spark.sql.DataFrame[0m = []'''

cleaned = re.sub('\[[0-9;]+[a-zA-Z]',' ',dirty)
print(cleaned)

But it may sometimes remove more than necessary. See also discussion of: Removing colors from output

It would be more reliable to disable terminal coloring, but sometimes it is not the option.

Maciej Wrobel
  • 640
  • 4
  • 11