[英]Python. Convert string to dictionary containing numpy arrays
I have a string which has this structure:我有一个具有这种结构的字符串:
{0: [array([5.1, 3.5, 1.4, 0.2]), array([4.9, 3. , 1.4, 0.2]), 1: [array([7. , 3.2, 4.7, 1.4]), array([6.4, 3.2, 4.5, 1.5]), 2: [array([6.3, 3.3, 6. , 2.5]), array([7.1, 3. , 5.9, 2.1])]}
It is in the form of a python dictionary containing numpy arrays.它采用 python 字典的形式,其中包含 numpy arrays。 How can I turn this string into a python dictionary containing numpy arrays?如何将此字符串转换为包含 numpy arrays 的 python 字典?
Thanks for the replies already.感谢您的回复。 I can't simply just change the string because the string is an output of a python file that I want to pipe to another one.我不能简单地更改字符串,因为该字符串是 python 文件的 output 我想将 pipe 转换为另一个文件。 I could go and change the format of this output, but I was rather hoping to avoid that.我可以 go 并更改此 output 的格式,但我宁愿避免这种情况。
In an interactive ipython
session is created an alias: In [7]: array = np.array在交互式ipython
session 中创建了一个别名:在 [7] 中:array = np.array
and edited a copy-n-paste of your sample.并编辑了样本的复制粘贴。 I had to add a couple of closing ]
:我不得不添加几个关闭]
:
In [8]: data = {0: [array([5.1, 3.5, 1.4, 0.2]), array([4.9, 3. , 1.4, 0.2])], 1
...: : [array([7. , 3.2, 4.7, 1.4]), array([6.4, 3.2, 4.5, 1.5])], 2: [array(
...: [6.3, 3.3, 6. , 2.5]), array([7.1, 3. , 5.9, 2.1])]}
Now a simple execute works:现在一个简单的执行工作:
In [9]: data
Out[9]:
{0: [array([5.1, 3.5, 1.4, 0.2]), array([4.9, 3. , 1.4, 0.2])],
1: [array([7. , 3.2, 4.7, 1.4]), array([6.4, 3.2, 4.5, 1.5])],
2: [array([6.3, 3.3, 6. , 2.5]), array([7.1, 3. , 5.9, 2.1])]}
or with a string (corrected):或带有字符串(已更正):
In [10]: astr="{0: [array([5.1, 3.5, 1.4, 0.2]), array([4.9, 3. , 1.4, 0.2])], 1
...: : [array([7. , 3.2, 4.7, 1.4]), array([6.4, 3.2, 4.5, 1.5])], 2: [array
...: ([6.3, 3.3, 6. , 2.5]), array([7.1, 3. , 5.9, 2.1])]}"
In [11]: astr
Out[11]: '{0: [array([5.1, 3.5, 1.4, 0.2]), array([4.9, 3. , 1.4, 0.2])], 1: [array([7. , 3.2, 4.7, 1.4]), array([6.4, 3.2, 4.5, 1.5])], 2: [array([6.3, 3.3, 6. , 2.5]), array([7.1, 3. , 5.9, 2.1])]}'
I can do an exec
or eval
:我可以执行exec
或eval
:
In [15]: eval(astr)
Out[15]:
{0: [array([5.1, 3.5, 1.4, 0.2]), array([4.9, 3. , 1.4, 0.2])],
1: [array([7. , 3.2, 4.7, 1.4]), array([6.4, 3.2, 4.5, 1.5])],
2: [array([6.3, 3.3, 6. , 2.5]), array([7.1, 3. , 5.9, 2.1])]}
Use of eval
is often discouraged because it can be hacked.通常不鼓励使用eval
,因为它可能会被黑客入侵。 But the safer ast_eval
only works with dict, list and tuples, not np.array
.但更安全的ast_eval
仅适用于 dict、list 和 tuples,而不np.array
。 One way or other you have to edit the string so it is a valid Python/numpy expression.您必须以一种或其他方式编辑字符串,使其成为有效的 Python/numpy 表达式。
I changed how the string was being being produced so that it was in the form of a dictionary containing python lists instead of numpy arrays.我更改了字符串的生成方式,使其采用包含 python 列表的字典形式,而不是 numpy arrays。 With this I could just use ast.literal_eval()
to deseriealize it.有了这个,我可以使用ast.literal_eval()
来反序列化它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.