运行 backslash/escape 个已打开文件中的字符

Run backslash/escape character from file opened

我想读取文件和 运行 存储在文件中的反斜杠“命令”\x1b[92m

我得到一个如下所示的文件:

\x1b[92m this text is cyan

Python:

f = open(f"./banners/file", "r")
content = f.read()
f.close()

print(content)

输出支持os以青色表示“此文本为青色”,但它只是像这样打印出来 \x1b[92m this text is cyan

只是为了证明它适用于我的 os...

如果我这样设置颜色代码

print("\x1b[92m this is cyan")

它以青色打印。

f.read() 转义反斜杠字符,因此 content = '\x1b[92m this text is cyan\n',当您实际打印出来时。相反,在打开文件时执行 f=open('t.txt', 'r', encoding='unicode_escape') 所以它会忽略转义字符