Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19k points)

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 3\n 4.5 8" numpy.genfromtxt(io.StringIO(x))

I get the following error:

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

and when I write import StringIO it says  

ImportError: No module named 'StringIO'

2 Answers

0 votes
by (106k points)

You should import stringIo as follows:-

from io import StringIO

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

try:

   from StringIO import StringIO

except ImportError:

   from io import StringIO

0 votes
by (20.3k points)

If you are working on Python 3 numpy.genfromtxt expects a bytes stream. You can try using this:

numpy.genfromtxt(io.BytesIO(x.encode()))

Related questions

0 votes
4 answers
0 votes
4 answers
asked Apr 11, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Dec 19, 2020 in Python by laddulakshana (16.4k points)

Browse Categories

...