Adapt an iterator to behave like a file-like object in Python

前端 未结 8 1139
Happy的楠姐
Happy的楠姐 2020-12-13 07:00

I have a generator producing a list of strings. Is there a utility/adapter in Python that could make it look like a file?

For example,

>>> d         


        
8条回答
  •  借酒劲吻你
    2020-12-13 07:31

    There is one called werkzeug.contrib.iterio.IterIO but note that it stores the entire iterator in its memory (up to the point you have read it as a file) so it might not be suitable.

    http://werkzeug.pocoo.org/docs/contrib/iterio/

    Source: https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/contrib/iterio.py

    An open bug on readline/iter: https://github.com/mitsuhiko/werkzeug/pull/500

提交回复
热议问题