简体   繁体   中英

How do I convert a B string to bytes?

bstr = "b'\\xe4\\xb8\\x96\\xe7\\x95\\x8c'"
bbytes = b'\\xe4\\xb8\\x96\\xe7\\x95\\x8c'

I want to convert bstr to bbytes , how can I do?

You can use the ast.literal_eval (documentation here ) function to evaluate this string as a python literal.

import ast

bstr = "b'\\xe4\\xb8\\x96\\xe7\\x95\\x8c'"
bbytes = ast.literal_eval(bstr)
print(bbytes)  # Outputs: b'\xe4\xb8\x96\xe7\x95\x8c'

This function should be safe to use on user inputs (unlike eval ), though you should probably enforce a length limit to address the warning about crashing the interpreter with long/complex inputs.

Note this will also correctly parse other valid python literals (such as int , list , etc.), so if you want to enforce that you only end up with bytes you should check that, eg

if not isinstance(bbytes, bytes):
  raise ValueError("Input must be a bytes string")

Hopefully you can change the input slightly, I changed the input to escape bstr so the special characters aren't evaluated immediately.

If you're taking this string as user input, eg from input or from reading a file, this should already be the case.

If you don't have a properly escaped input, you'll get an exception:

>>> bstr = "b'\xe4\xb8\x96\xe7\x95\x8c'"
>>> ast.literal_eval(bstr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/ast.py", line 48, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python3.6/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
SyntaxError: bytes can only contain ASCII literal characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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