将连接的对象编码和解码为流
streamcat的Python项目详细描述
将json迭代器编码为流:
defgen_records():yieldb'{"foo": "bar"}'yieldb'{"baz": [1, 2, 3]}'stream=streamcat.iterator_to_stream(gen_records())# `stream` can then be used just like any other `io.RawIOBase`withopen('/tmp/jsoncat','wb')asdestination:shutil.copyfileobj(stream,destination)
将流解码为生成器:
decoder=json.JSONDecoder()withopen('/tmp/jsoncat','rb')assource:records=streamcat.stream_to_iterator(source,decoder)forrecordinrecords:print(record)