简体   繁体   English

如何将带有特殊控制字符(例如\\'x1b')的字符串转换为标准字符串?

[英]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.

相关问题 如何将“ \\ x1b”转换为真实的控制字符“ [Esc]” - How to convert “\x1b” into a real control character “[Esc]” 如何从python中的字符串中删除\\ x1b - How to remove \x1b from a string in python 如何在 Python 中将字符串“3x a;2x b”转换为字符串“aaabb”? - How can I convert string like '3x a;2x b' to string 'a a a b b' in Python? Python将特殊的unicode字符(例如♡)转换为字符串中的十六进制文字(例如0x2661) - Python to convert special unicode characters (like ♡) to their hexadecimal literals in a string (like 0x2661) 如何将\\ x22之类的字符转换为字符串? - How to convert characters like \x22 into a string? 如何在Python中将0x1b87打印为\\ x1b \\ x87? - How can I get 0x1b87 to print like \x1b\x87 in Python? 如何将包含控制字符的字符串转换为应用了控制字符的字符串? - How to convert a string containing control characters to a string with the control characters applied? 将字符串转换为带有特殊字符的unicode - Convert a string to unicode with special characters \\\\ x1b(B`是做什么的? - What does `\x1b(B` do? 如何使用 Python 在带有正则表达式的字符串中搜索/查找特殊字符,如 &amp;、&lt; 或 &gt; - How to search/find special characters like &, < or > in the string with regex using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM