Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

io.StringIO encoding in python3

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.

like image 607
Giovanni Funchal Avatar asked Feb 20 '12 21:02

Giovanni Funchal


People also ask

What is StringIO python3?

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.

How do I write a StringIO file?

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.

What does io Bytesio do in Python?

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.

What is _IO TextIOWrapper in Python?

TextIOWrapper , which extends TextIOBase , is a buffered text interface to a buffered raw stream ( BufferedIOBase ). Finally, StringIO is an in-memory stream for text.


1 Answers

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.)

like image 148
Sven Marnach Avatar answered Sep 27 '22 01:09

Sven Marnach