Py学习  »  Python

Python3将用户输入字符串解释为原始字节(例如。\x41==“A”)

BitWrecker • 3 年前 • 1553 次点击  
  • 我想使用 input() 函数,我希望用户提供如下输入 \x41\x42\x43 输入“ABC”。用户必须以字节格式输入,但不能提供与之等效的字母数字。

  • 我的问题是,当我接收用户输入,然后打印出来时,我看到python试图用另一个反斜杠来逃避反斜杠,因此它无法将其解释为ASCII中表示的字节。

Python3命令提示符中的示例代码:

1 | >>> var_abc = "\x41\x42\x43"
2 | >>> print(var_abc)
3 | ABC
4 | >>> print(bytes(var_abc, encoding='ascii'))
5 | b'ABC'

6 | >>> user_input_abc = input('enter user input in bytes: ')
7 | enter user input in bytes: \x41\x42\x43
8 | >>> print(user_input_abc)
9 | \x41\x42\x43
10| >>> print(bytes(user_input_abc, encoding='ascii'))
11| b'\\x41\\x42\\x43'
  • 我希望第11行的输出与第5行的输出相同。 我需要做什么才能让python将我的用户输入解释为原始字节,而不转义前面的每个反斜杠?
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133366
 
1553 次点击  
文章 [ 1 ]  |  最新文章 3 年前