0 votes
1 view
in Python by (17.4k 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 (107k 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 Dec 19, 2020 in Python by laddulakshana (9.3k points)
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...