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'
回溯(最近一次调用最后一次):TypeError中的文件“”,第1行:需要类似字节的对象,而不是'str'
1 个解决方案
#1
1
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.]])
#1
1
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.]])