Serializing binary data in Python -
i have binary data in python in form of array of byte strings.
is there portable way serialize data other languages read?
json loses because found out has no real way store binary data; strings expected unicode.
i don't want use pickle because don't want security risk, , limits use other python programs.
any advice? use builtin library (or @ least 1 that's part of standard anaconda distribution).
if need binary data in strings , can recover boundaries between individual strings easily, write them file directly, raw strings.
if can't recover string boundaries easily, json seems option:
a = [b"abc\xf3\x9c\xc6", b"xyz"] serialised = json.dumps([s.decode("latin1") s in a]) print [s.encode("latin1") s in json.loads(serialised)] will print
['abc\xf3\x9c\xc6', 'xyz'] the trick here arbitrary binary strings valid latin1, can decoded unicode , encoded original string again.
Comments
Post a Comment