1

My sample code will be like this

import numpy as np
from io import BytesIO

data = "1, 2, 3\n4, 5, 6"
np.genfromtxt(data, delimiter=",")

while run this code throws error

Traceback (most recent call last): File "", line 1, in TypeError: a bytes-like object is required, not 'str'

3
  • 1
    I think you want np.genfromtxt(BytesIO(data), delimiter=",") the error suggests it doesn't like the string as is Commented Dec 31, 2017 at 3:24
  • shouldn't it be read as BytesIO(data.encode())? Commented Dec 31, 2017 at 3:25
  • thiswould be helpful Commented Dec 31, 2017 at 3:26

1 Answer 1

2

Encode the string before reading it:

data = "1, 2, 3\n4, 5, 6"
np.genfromtxt(BytesIO(data.encode()), delimiter=",")

array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.