Stringio in python3

744    Asked by RutujaMishra in Python , Asked on Apr 11, 2021

I am using Python 3.2.1 and I can't import the StringIO module. I use io.StringIO and it works, but I can't use it with numpy's genfromtxt like this:

x="1 3n 4.5 8" numpy.genfromtxt(io.StringIO(x))

I get the following error: no module named 'stringio'

TypeError: Can't convert 'bytes' object to str implicitly

and when I write import StringIO it says  

ImportError: No module named 'StringIO'

Answered by Rutuja Mishra

You should import stringIo as follows:-

from io import StringIO

Below is the code that can be used for fixing the given error in Python 3 is as follows:

try:

   from StringIO import StringIO

except ImportError:

   from io import StringIO



Your Answer

Interviews

Parent Categories