如何转换以下字符串文字:
token = "\x1b(0l\x1b(BHeader"
分为:
"┌Header"
我从库中接收文字Unix框图字符,需要转换为unicode进行单元测试。
答案 0 :(得分:6)
这是VT-100备用字符集(在编辑问题主题之前不太明显)。序列assemblyMergeStrategy in assembly := {
^
C:\Users\sreer\Desktop\workspace\cool\build.sbt:14: error: not found:
value assembly
assemblyMergeStrategy in assembly := {
^
C:\Users\sreer\Desktop\workspace\cool\build.sbt:15: error: not found:
value PathList
case PathList("META-INF", xs @ _*) => MergeStrategy.discard
^
C:\Users\sreer\Desktop\workspace\cool\build.sbt:15: error: star patterns
must correspond with varargs parameters
case PathList("META-INF", xs @ _*) => MergeStrategy.discard
^
C:\Users\sreer\Desktop\workspace\cool\build.sbt:15: error: not found:
value MergeStrategy
case PathList("META-INF", xs @ _*) => MergeStrategy.discard
^
C:\Users\sreer\Desktop\workspace\cool\build.sbt:16: error: not found:
value MergeStrategy
case x => MergeStrategy.first
^
[error] Type error in expression
将编码更改为备用集,序列esc ( 0
将其更改回来。只有少数几个这样的字符被映射。这是映射。
esc ( B
要进行转换,您必须设置一个dict,将这些代码映射到相应的Unicode代码点并自行转换。
答案 1 :(得分:1)
@BoarGules是正确的。这是我解码字符串的解决方案:
vt_100_mapping = {
'0x71': '─',
'0x74': '├',
'0x75': '┤',
'0x76': '┴',
'0x77': '┬',
'0x78': '│',
'0x6a': '┘',
'0x6b': '┐',
'0x6c': '┌',
'0x6d': '└',
'0x6e': '┼',
}
from itertools import groupby
def decode_vt_100(iterable, default_set='(B', alt_set='(0'):
for is_escape, group in groupby(iterable, lambda _: _ =='\x1b'):
if is_escape:
continue
characters = ''.join(group)
if characters.startswith(default_set):
yield characters[len(default_set):]
elif characters.startswith(alt_set):
for character in characters[len(alt_set):]:
yield vt_100_mapping[hex(ord(character))]
>>> print(''.join(decode_vt_100("\x1b(0l\x1b(BHeader")))
┌Header