Dumping Json Directly Into A Tarfile
I have a large list of dict objects. I would like to store this list in a tar file to exchange remotely. I have done that successfully by writing a json.dumps() string to a tarfile
Solution 1:
Why do you think you can write a tarfile to a StringIO? That doesn't work like you think it does.
This approach doesn't error, but it's not actually how you create a tarfile in memory from in-memory objects.
from json import dumps
from io import BytesIO
import tarfile
data = [{'foo': 'bar'},
{'cheese': None},
]
filename = 'fnord'with BytesIO() as out_stream, tarfile.open(filename, 'w|gz', out_stream) as tar_file:
for packet in data:
out_stream.write(dumps(packet).encode())
Post a Comment for "Dumping Json Directly Into A Tarfile"