Sakthi Panneerselvam
Sakthi Panneerselvam

Reputation: 1397

Numpy throws error while using genfromtxt function in python 3

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'

Upvotes: 1

Views: 563

Answers (1)

skrubber
skrubber

Reputation: 1105

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.]])

Upvotes: 2

Related Questions