[英]How to convert string with special control characters, like \'x1b', into a standard string?
When I use python socket program, we give an option like: 当我使用python套接字程序时,我们提供了以下选项:
1) Input A to show your name
2) Input B to show your age
3) Input other to set your name
>>
When client types 'Too' + delete button + 'm', server receives 'Too\\x1bm'. 当客户端键入'Too'+删除按钮+'m'时,服务器会收到'Too \\ x1bm'。
How do I convert 'Too\\x1bm' to 'Tom' in Python? 如何在Python中将“ Too \\ x1bm”转换为“ Tom”?
There may also be other control characters like 'move cursor' and 'tab'. 可能还有其他控制字符,例如“移动光标”和“选项卡”。
If you know all the 'wrong characters' you can use .replace to remove unwanted parts. 如果您知道所有“错误字符”,则可以使用.replace删除不需要的部分。
'Too\x1bm'.replace(a[a.index('\x1b')-1:a.index('\x1b')+1],'')
returns >>> Tom
My first guess would be: 我的第一个猜测是:
line = 'Too\x1bm'
if '\x1b' in line:
while True:
index = line.find('\x1b')
if index > 0:
line = line[:index - 1] + line[index + 1:]
else:
break
line = line.replace('\x1b', '')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.