Created
September 24, 2024 11:59
-
-
Save moreati/66d88c58000fc5cc2e67c254bb5d0e53 to your computer and use it in GitHub Desktop.
Python 2.x StringIO.StringIO vs io.StringIO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#1/usr/bin/env python2 | |
import StringIO | |
f1 = StringIO.StringIO() | |
f1.write(u'unicode string') | |
f1.write(b'byte string') | |
f1.close() | |
import io | |
f2 = io.StringIO() | |
f2.write(u'unicode string') | |
f2.write(b'byte string') | |
f2.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
StringIO.StringIO.write()
accepts both byte strings and unicode strings, similar to Python 2.xopen()
.io.StringIO.write()
only accepts unicode strings, similar toio.open(... 'w', encoding=...)
or Python 3.xopen()