How to declare a byte array contains non-ascii characters without escape in python 3

Viewed 11450

Here is an example I wrote in python2

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys, struct

def pack(*s_list):
    return b"".join(struct.pack(">I", len(s)) + s for s in s_list)

if __name__ == "__main__":
    print(sys.version)
    a = pack("α", "привет мир", "±")
    b = b"\x00\x00\x00\x02α\x00\x00\x00\x13привет мир\x00\x00\x00\x02±"
    print(a == b)

And the converted code for python 3,

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, struct

def pack(*s_list):
    return b"".join(struct.pack(">I", len(b)) + b for b in (s.encode() for s in s_list))

if __name__ == "__main__":
    print(sys.version)
    a = pack("α", "привет мир", "±")
    b = b"\x00\x00\x00\x02α" + "α".encode() + b"\x00\x00\x00\x13" + "привет мир".encode() + b"\x00\x00\x00\x02" + "±".encode()
    print(a == b)

I notice use b = b"\x00\x00\x00\x02α\x00\x00\x00\x13привет мир\x00\x00\x00\x02±" in python 3 will get an exception SyntaxError: bytes can only contain ASCII literal characters., so I had to escape them all or use many + like previous code.

Is there a better way to declare a byte array contains non-ascii character in python3?

5 Answers

Another solution:

b = bytes("привет мир", 'utf-8')

Seems it is the better solution, since it has lesser copy operations.

Related