我正试图在curses
python应用程序中显示文件中的彩色文本。该文件包含24位彩色转义码,例如:^[[48;2;255;255;255m^[[38;2;255;255;255mA
。我需要循环遍历该文件并生成一个元组列表,其中包含如下元组:((background: r, g, b), (foreground: r, g, b), char)
。这是我迄今为止尝试过的代码。它试图找到所有的0x27字节并对其进行解析,但它似乎不起作用,不仅是因为它不是特别安全/可扩展/可维护。我如何解析像这样的文件中的颜色代码?有图书馆吗?如果没有库,我该如何改进此代码?
def read_until(char, data, pos):
"""read from a bytes-like object starting at `pos` until the target character is found"""
start = pos
while data[pos] != char: pos += 1
return data[start:pos], pos
def makeData(data):
assert type(data) == bytes
out = []
pos = 0
bg = (0, 0, 0)
fg = (255, 255, 255)
while pos < len(data):
if data[pos] == 27: #start of the escape sequence
pos += 2 #+2 to ignore the `[` char
if data[pos:pos + 2] == b"0m": #reset character, just ignore it
continue
first_num, pos = read_until(ord(";"), data, pos)
second_num, pos = read_until(ord(";"), data, pos + 1) #+ 1 for the `;` char
r, pos = read_until(ord(";"), data, pos + 1)
g, pos = read_until(ord(";"), data, pos + 1)
b, pos = read_until(ord("m"), data, pos + 1)
r = int(r)
g = int(g)
b = int(b)
pos += 1 #skip last `m` char
if first_num == b"48": #48 means foreground
fg = (r, g, b)
elif first_num == b"38": #38 means background
bg = (r, g, b)
else:
out.append((fg, bg, chr(data[pos]))) #use current fg and bg colours with char
pos += 1
return out
with open("file.txt", "rb") as f:
print("".join([chr(i[2]) for i in makeData(f.read())])) #print without colour codes, to test
您可以使用正则表达式来解析数据:
import re
with open("file.txt") as f:
result = re.findall(r'x1b[48;2;(d+);(d+);(d+)mx1b[38;2;(d+);(d+);(d+)m(.)', f.read())
print(result)