[英]Convert string to 2 dimensional numpy array
I have a string which is loaded from a .mat file and is in the following format :我有一个从 .mat 文件加载的字符串,格式如下:
(array([172.169, 73.2]), array([128.83, 102.31]), array([143.49, 124.43]), array([186.83, 95.69]), 'R', array([], dtype=float64))
Is there any way I can convert this directly to a numpy 2D array without parsing over the entire string and removing the "arrays" manually?有什么方法可以将其直接转换为 numpy 2D 数组,而无需解析整个字符串并手动删除“数组”? Neither
numpy.fromstring
nor numpy.frombuffer
will work here. numpy.fromstring
和numpy.frombuffer
都不能在这里工作。
you can use np.fromstring
with a regular expression:您可以将
np.fromstring
与正则表达式一起使用:
import re
# s is your string
np.fromstring(', '.join(re.findall(r'\[(.+?)\]', s)), sep =', ').reshape((2, 2, 2))
output:输出:
array([[[172.169, 73.2 ],
[128.83 , 102.31 ]],
[[143.49 , 124.43 ],
[186.83 , 95.69 ]]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.