1

I am trying to print MAC address using python rich library. Below is code. The ":cd" in the MAC address get converted to an actual CD disk emoji. How to prevent that from happening?

from rich.console import Console
from rich.table import Table

table = Table(safe_box=True)
table.add_column("MAC address")
table.add_row("08:00:27:cd:af:88")
console = Console()
console.print(table)

Output:

┏━━━━━━━━━━━━━━━━━┓
┃ MAC address     ┃
┡━━━━━━━━━━━━━━━━━┩
│ 08:00:27af:88 │
└─────────────────┘

I tried using safe_box=True option to not print Unicode but that did not work. I want the final output to look like

┏━━━━━━━━━━━━━--━━━━┓
┃ MAC address       ┃
┡━━━━━━━━━━━━━━━--━━┩
│ 08:00:27:cd:af:88 │
└───────────--──────┘

1 Answers1

0

The documentation describes this. You use backslash to escape characters that would otherwise be recognized.

table.add_row("08:00:27\\:cd:af:88")

If you have a string, do

s = s.replace(':cd','\\:cd')

https://rich.readthedocs.io/en/stable/markup.html

FOLLOWUP

I looked at the full emoji list in the source code. There are two possible paths.

First, they look for the Unicode "zero-width joiner" as a way to interrupt the emoji search. So, you could do:

s = s.replace(':cd',':\u200dcd')

Second, "cd" is, in fact, the only two-letter emoji code in their list that is also a hex number. So, you can add this at the top, and this also works:

from rich._emoji_codes import EMOJI
del EMOJI["cd"]

I've tested both of these. What a very strange problem.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30