简体   繁体   English

如何从 Python3 中的 input() 获取字节数组?

[英]How can I take bytes array from input() in Python3?

I trying to make pretty simple script to decode bytes to UUID.我试图制作非常简单的脚本来将字节解码为 UUID。

I have byte arrays like \223ge\254\367\217F\303\206\363\313H\222\207\362\216我有字节 arrays 像\223ge\254\367\217F\303\206\363\313H\222\207\362\216

I want to run python3 script from terminal, paste to input this array and see printed result of this code:我想从终端运行 python3 脚本,粘贴以输入此数组并查看此代码的打印结果:

b_str = b'\223ge\254\367\217F\303\206\363\313H\222\207\362\216'
    print(UUID(bytes=b_str))

This example works fine and gives me this:这个例子工作得很好,给了我这个:
936765ac-f78f-46c3-86f3-cb489287f28e

But when I trying to write down and run something like this:但是当我试图写下并运行这样的东西时:

from uuid import UUID


def encode_client_id():
    bytes_str = input('Paste bytes string from decoded blob here: ')
    print(UUID(bytes=bytes_str))


if __name__ == '__main__':
    encode_client_id()

I always get this exception:我总是得到这个例外:

(venv) username@machinename:$ python3 main.py 
Paste bytes string from decoded blob here: \223ge\254\367\217F\303\206\363\313H\222\207\362\216
Traceback (most recent call last):
  File "main.py", line 11, in <module>
    encode_client_id()
  File "main.py", line 7, in encode_client_id
    print(UUID(bytes=bytes_str))
  File "/usr/lib/python3.8/uuid.py", line 178, in __init__
    raise ValueError('bytes is not a 16-char string')
ValueError: bytes is not a 16-char string
(venv) username@machinename:~/digitex/Dev/client_id_parser$ 

I understand that input() function always return string type, but I can't find any info how to convert string to bytes or take from input (terminal especially) byte array.我知道input() function 总是返回字符串类型,但我找不到任何信息如何将字符串转换为字节或从输入(尤其是终端)字节数组中获取。

When I trying this code to transform string to bytes (with the same bytes array pasted to input):当我尝试使用此代码将字符串转换为字节时(将相同的字节数组粘贴到输入):

bytes_str = input('Paste bytes string from decoded blob here: ')
    bytes_str = bytes(bytes_str, encoding='utf-8')
    print(bytes_str)

I get this:我明白了:

Paste bytes string from decoded blob here: \223ge\254\367\217F\303\206\363\313H\222\207\362\216
b'\\223ge\\254\\367\\217F\\303\\206\\363\\313H\\222\\207\\362\\216'
Traceback (most recent call last):
  File "main.py", line 12, in <module>
    encode_client_id()
  File "main.py", line 8, in encode_client_id
    print(UUID(bytes=bytes_str))
  File "/usr/lib/python3.8/uuid.py", line 178, in __init__
    raise ValueError('bytes is not a 16-char string')
ValueError: bytes is not a 16-char string

Please help yet another newbie who can't handle simple issue:)请帮助另一个无法处理简单问题的新手:)

Surely there is a better way, but this is the best I can come up with for the moment:当然有更好的方法,但这是我目前能想到的最好的方法:

from ast import literal_eval
from uuid import UUID

def encode_client_id():
    bytes_str = input('Paste bytes string from decoded blob here: ')
    bytes_str = literal_eval(f"b'{bytes_str}'")
    print(UUID(bytes=bytes_str))

>>> encode_client_id()
Paste bytes string from decoded blob here: \223ge\254\367\217F\303\206\363\313H\222\207\362\216
936765ac-f78f-46c3-86f3-cb489287f28e

This wraps the input string in quotes and prepends a b character to make the input a Python literal byte string.这会将输入字符串括在引号中并在前面添加一个b字符以使输入成为 Python 文字字节字符串。 Then it calls literal_eval() on the doctored string to convert to an actual byte string that will be accepted by UUID() .然后它在修改后的字符串上调用literal_eval()以转换为UUID()将接受的实际字节字符串。

There must be something better?一定有更好的东西吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM