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'

Sakthi Panneerselvam
  • 1,337
  • 1
  • 14
  • 25

1 Answers1

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.]])
skrubber
  • 1,095
  • 1
  • 9
  • 18