小编Per*_*l W的帖子

将控制字符应用于字符串 - Python

我正在尝试将控制字符(例如应该删除先前字符的'\ x08\x08)应用于字符串(向后移动,写入空格,向后移动)
例如当我键入python控制台时:

s = "test\x08 \x08"
print s
print repr(s)
Run Code Online (Sandbox Code Playgroud)

我进入我的终端:

tes
'test\x08 \x08'
Run Code Online (Sandbox Code Playgroud)

我正在寻找一个函数,让我们说"函数",它将"应用"控制字符到我的字符串:

v = function("test\x08 \x08")
sys.stdout.write(v)
sys.stdout.write(repr(v))
Run Code Online (Sandbox Code Playgroud)

所以我得到一个"干净",无控制字符的字符串:

tes
tes
Run Code Online (Sandbox Code Playgroud)

我理解在终端中,这部分是由客户端处理的,所以可能有一种方法来获取显示的字符串,使用核心unix函数

echo -e 'test\x08 \x08'
cat file.out # control char are here handled by the client
>> tes
cat -v file.out # which prints the "actual" content of the file
>> test^H ^H
Run Code Online (Sandbox Code Playgroud)

python string repr control-characters backspace

5
推荐指数
1
解决办法
2347
查看次数

标签 统计

backspace ×1

control-characters ×1

python ×1

repr ×1

string ×1