繁体   English   中英

如何将 Python 字节字符串表示形式转换为字节?

[英]How to convert Python bytes string representation to bytes?

我在一个文本文件中存储了许多 Python 字节对象,Python 打印的结果类似于"b'\\x80\\x03}q\\x00.'"如何将这些对象中的每一个都转换回字节对象?

换句话说,我试图找到一个函数convert("b'\\x80\\x03}q\\x00.'") == b'\\x80\\x03}q\\x00.' .

我觉得这应该是微不足道的,但这些明显的方法都不起作用:

>>> s = "b'\x80\x03}q\x00.'"
>>> bytes(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string argument without an encoding
>>> bytes(s.encode())
b"b'\xc2\x80\x03}q\x00.'"
>>> bytes(s[2:-1].encode())
b'\xc2\x80\x03}q\x00.'
>>> bytes(s[2:-1].encode('utf8'))
b'\xc2\x80\x03}q\x00.'
>>> eval(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: source code string cannot contain null bytes
>>> exec(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: source code string cannot contain null bytes

这并不真正适用于从文件中读取s值的情况,但在您的示例中,常规字符串文字扩展了转义序列:

>>> s = "b'\x80\x03}q\x00.'"
>>> list(s)
['b', "'", '\x80', '\x03', '}', 'q', '\x00', '.', "'"]

请注意, s不包含空字节的转义序列; 它包含一个实际的空字节。

您可以使用原始字符串文字来避免这种情况:

>>> s = r"b'\x80\x03}q\x00.'"
>>> list(s)
['b', "'", '\\', 'x', '8', '0', '\\', 'x', '0', '3', '}', 'q', '\\', 'x', '0', '0', '.', "'"]

在这种情况下ast.literal_eval是您正在寻找的函数:

>>> ast.literal_eval(s)
b'\x80\x03}q\x00.'

原始字符串文字应该产生您将从文件中读取的值:

import ast

b = b'\x80\x03}q\x00.'

with open("tmp.txt", "w") as f:
    print(str(b), file=f)

with open("tmp.txt") as f:
    s = f.readline().strip()

assert ast.literal_eval(s) == b

暂无
暂无

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

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