Wrap an open stream with io.TextIOWrapper

后端 未结 6 1674
太阳男子
太阳男子 2020-12-05 13:16

How can I wrap an open binary stream – a Python 2 file, a Python 3 io.BufferedReader, an io.BytesIO – in an io.TextIOWrapper

6条回答
  •  爱一瞬间的悲伤
    2020-12-05 13:57

    Here's some code that I've tested in both python 2.7 and python 3.6.

    The key here is that you need to use detach() on your previous stream first. This does not close the underlying file, it just rips out the raw stream object so that it can be reused. detach() will return an object that is wrappable with TextIOWrapper.

    As an example here, I open a file in binary read mode, do a read on it like that, then I switch to a UTF-8 decoded text stream via io.TextIOWrapper.

    I saved this example as this-file.py

    import io
    
    fileName = 'this-file.py'
    fp = io.open(fileName,'rb')
    fp.seek(20)
    someBytes = fp.read(10)
    print(type(someBytes) + len(someBytes))
    
    # now let's do some wrapping to get a new text (non-binary) stream
    pos = fp.tell() # we're about to lose our position, so let's save it
    newStream = io.TextIOWrapper(fp.detach(),'utf-8') # FYI -- fp is now unusable
    newStream.seek(pos)
    theRest = newStream.read()
    print(type(theRest), len(theRest))
    

    Here's what I get when I run it with both python2 and python3.

    $ python2.7 this-file.py 
    (, 10)
    (, 406)
    $ python3.6 this-file.py 
     10
     406
    

    Obviously the print syntax is different and as expected the variable types differ between python versions but works like it should in both cases.

提交回复
热议问题