I can't seem to find what's the default encoding for io.StringIO
in Python3. Is it the locale as with stdio
?
How can I change it?
With stdio
, seems that just reopening with correct encoding works, but there's no such thing as reopening a StringIO
.
The StringIO module is an in-memory file-like object. This object can be used as input or output to the most function that would expect a standard file object. When the StringIO object is created it is initialized by passing a string to the constructor. If no string is passed the StringIO will start empty.
Write to A Memory File As the above example shown, we can use the write() method to write data to a StringIO object. The getvalue() method helps us get the entire content of a StringIO object.
It takes input POSIX based arguments and returns a file descriptor which represents the opened file. It does not return a file object; the returned value will not have read() or write() functions. Overall, io.
TextIOWrapper , which extends TextIOBase , is a buffered text interface to a buffered raw stream ( BufferedIOBase ). Finally, StringIO is an in-memory stream for text.
The class io.StringIO
works with str
objects in Python 3. That is, you can only read and write strings from a StringIO
instance. There is no encoding -- you have to choose one if you want to encode the strings you got from StringIO
in a bytes
object, but strings themselves don't have an encoding.
(Of course strings need to be internally represented in some encoding. Depending on your interpreter, that encoding is either UCS-2 or UCS-4, but you don't see this implementation detail when working with Python.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With