Python 将字符串更改为逗号分隔的numpy int数组

Python 将字符串更改为逗号分隔的numpy int数组,python,python-3.x,numpy,Python,Python 3.x,Numpy,我有一个以字节为单位的字符串,里面有逗号 例如b'-8,0,54,-30,28' 我首先使用 msg = str(msg, 'utf-8') 这部分是有效的。但是我需要将这个字符串转换成一个numpy int数组。我尝试过用逗号拆分,但最终得到的是一个一维numpy数组。我希望数组中的每个值都用逗号分隔 msg = str(msg, 'utf-8') z = [x.strip() for x in msg.split(',')] x = np.array(z) y = x.astype(n

我有一个以字节为单位的字符串,里面有逗号

例如
b'-8,0,54,-30,28'

我首先使用

msg = str(msg, 'utf-8')
这部分是有效的。但是我需要将这个字符串转换成一个numpy int数组。我尝试过用逗号拆分,但最终得到的是一个一维numpy数组。我希望数组中的每个值都用逗号分隔

msg = str(msg, 'utf-8')

z = [x.strip() for x in msg.split(',')]

x = np.array(z)
y = x.astype(np.int)
我得到的错误是

ValueError: Error when checking input: expected dense_1_input to have shape (5,) but got array with shape (1,)

谢谢你的帮助

列表中缺少的只是从字符串到int的转换:

msg=str(msg'utf-8')
z=[int(x.strip())表示msg.split(',')中的x]
x=np.数组(z)

split()
int()
能够将这些字符串转换为它们的数字表示形式。

您希望得到什么结果?你得到的“一维numpy数组”与你想要的“numpy int数组”有什么区别?@jasonharper我想要一个1乘5的数组,例如1行5列。这是因为数组被输入到神经网络中。
np.array(b'-8,0,54,-30,28'.decode().split('','),int)
产生
数组([-8,0,54,-30,28])
。形状是(5,)。如果您需要(1,5)或(5,1)之类的其他内容,请使用
重塑
。将
。重塑((1,5))
应用于阵列可以做到这一点,尽管错误消息听起来像是其他内容出错。谢谢您的回答!对不起,我不是很清楚。无论如何,要改变你的答案以反映我关于分离的问题中的编辑吗?
In [213]: b'-8 ,0 ,54 ,-30 ,28'.decode()                                                                     
Out[213]: '-8 ,0 ,54 ,-30 ,28'
In [214]: b'-8 ,0 ,54 ,-30 ,28'.decode().split(',')                                                          
Out[214]: ['-8 ', '0 ', '54 ', '-30 ', '28']
In [215]: np.array(b'-8 ,0 ,54 ,-30 ,28'.decode().split(','), dtype=int)                                     
Out[215]: array([ -8,   0,  54, -30,  28])
In [216]: np.array(b'-8 ,0 ,54 ,-30 ,28'.decode().split(','), dtype=int).reshape(-1,1)                       
Out[216]: 
array([[ -8],
       [  0],
       [ 54],
       [-30],
       [ 28]])