Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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
1 answer
asked Apr 11 in Python by laddulakshana (12.7k points)
0 votes
1 answer
asked Dec 19, 2020 in Python by laddulakshana (12.7k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...