从Mathematic导入到Python的数据

从Mathematic导入到Python的数据,python,import,wolfram-mathematica,Python,Import,Wolfram Mathematica,我有一个文本文件,其中包含从Mathematica获得的三维数组(100X100X100)。数据用逗号和大括号存储。我想用这个文本文件用Python分析和绘制数据。如何导入Python中的数据?我目前正在使用Python 2.7版本。在matematica中存储数据的格式可能是什么,以便在Python中使用它?因为看到人们用ascii存储如此多的数据让我感到痛苦,下面是一种进行二进制交换的方法: mathematica: data = RandomReal[{-1, 1}, {3, 4, 5}]

我有一个文本文件,其中包含从Mathematica获得的三维数组(100X100X100)。数据用逗号和大括号存储。我想用这个文本文件用Python分析和绘制数据。如何导入Python中的数据?我目前正在使用Python 2.7版本。在matematica中存储数据的格式可能是什么,以便在Python中使用它?

因为看到人们用ascii存储如此多的数据让我感到痛苦,下面是一种进行二进制交换的方法:

mathematica:

data = RandomReal[{-1, 1}, {3, 4, 5}]
f = OpenWrite["test.bin", BinaryFormat -> True];
BinaryWrite[f, ArrayDepth[data], "Integer32"];
BinaryWrite[f, Dimensions[data], "Integer32"];
BinaryWrite[f, data, "Real64"];
Close[f]
m = RandomReal[{-1, 1}, {8, 8}]
m >> out.m
python:

import numpy as np
with open('test.bin','rb') as f:
 depth=np.fromfile(f,dtype=np.dtype('int32'),count=1)
 dims =np.fromfile(f,dtype=np.dtype('int32'),count=depth)
 data =np.reshape(np.fromfile(f,dtype=np.dtype('float64'),
         count=reduce(lambda x,y:x*y,dims)),dims)
with open('out.m','r') as f: text=f.read()
for rep in (('{','['),('}',']')):text=text.replace(rep[0],rep[1])
array=eval(text)
注意:如果在不同的硬件上读/写,则可能会出现端号问题。 (不过很容易处理)

编辑:为完整起见,使用本机mathematica格式的文本交换如下所示:

mathematica:

data = RandomReal[{-1, 1}, {3, 4, 5}]
f = OpenWrite["test.bin", BinaryFormat -> True];
BinaryWrite[f, ArrayDepth[data], "Integer32"];
BinaryWrite[f, Dimensions[data], "Integer32"];
BinaryWrite[f, data, "Real64"];
Close[f]
m = RandomReal[{-1, 1}, {8, 8}]
m >> out.m
python:

import numpy as np
with open('test.bin','rb') as f:
 depth=np.fromfile(f,dtype=np.dtype('int32'),count=1)
 dims =np.fromfile(f,dtype=np.dtype('int32'),count=depth)
 data =np.reshape(np.fromfile(f,dtype=np.dtype('float64'),
         count=reduce(lambda x,y:x*y,dims)),dims)
with open('out.m','r') as f: text=f.read()
for rep in (('{','['),('}',']')):text=text.replace(rep[0],rep[1])
array=eval(text)

需要注意的是,第一个
eval
不应用于不真实的输入,第二个是,如果任何值使用科学记数法,或者显然是任何mathematica符号内容,这将中断。最后,对于大量的投入来说,它无疑是非常缓慢的。只有当您使用mathematica文件而无法使用mathematica将其转换为更好的格式时,我才会认真使用此文件。

因为看到人们以ascii格式存储如此多的数据让我感到痛苦,下面是一种进行二进制交换的方法:

mathematica:

data = RandomReal[{-1, 1}, {3, 4, 5}]
f = OpenWrite["test.bin", BinaryFormat -> True];
BinaryWrite[f, ArrayDepth[data], "Integer32"];
BinaryWrite[f, Dimensions[data], "Integer32"];
BinaryWrite[f, data, "Real64"];
Close[f]
m = RandomReal[{-1, 1}, {8, 8}]
m >> out.m
python:

import numpy as np
with open('test.bin','rb') as f:
 depth=np.fromfile(f,dtype=np.dtype('int32'),count=1)
 dims =np.fromfile(f,dtype=np.dtype('int32'),count=depth)
 data =np.reshape(np.fromfile(f,dtype=np.dtype('float64'),
         count=reduce(lambda x,y:x*y,dims)),dims)
with open('out.m','r') as f: text=f.read()
for rep in (('{','['),('}',']')):text=text.replace(rep[0],rep[1])
array=eval(text)
注意:如果在不同的硬件上读/写,则可能会出现端号问题。 (不过很容易处理)

编辑:为完整起见,使用本机mathematica格式的文本交换如下所示:

mathematica:

data = RandomReal[{-1, 1}, {3, 4, 5}]
f = OpenWrite["test.bin", BinaryFormat -> True];
BinaryWrite[f, ArrayDepth[data], "Integer32"];
BinaryWrite[f, Dimensions[data], "Integer32"];
BinaryWrite[f, data, "Real64"];
Close[f]
m = RandomReal[{-1, 1}, {8, 8}]
m >> out.m
python:

import numpy as np
with open('test.bin','rb') as f:
 depth=np.fromfile(f,dtype=np.dtype('int32'),count=1)
 dims =np.fromfile(f,dtype=np.dtype('int32'),count=depth)
 data =np.reshape(np.fromfile(f,dtype=np.dtype('float64'),
         count=reduce(lambda x,y:x*y,dims)),dims)
with open('out.m','r') as f: text=f.read()
for rep in (('{','['),('}',']')):text=text.replace(rep[0],rep[1])
array=eval(text)

需要注意的是,第一个
eval
不应用于不真实的输入,第二个是,如果任何值使用科学记数法,或者显然是任何mathematica符号内容,这将中断。最后,对于大量的投入来说,它无疑是非常缓慢的。如果你使用Mathematica文件,并且不能使用Mathematica来把它放在一个更好的格式上,我会认真地使用它。当然,这取决于文本文件的结构。您是如何导出它的?如果您有一个原生mathematica格式(逗号和大括号分隔的Id建议首先选择一个更友好的格式),它将以您提到的原生格式存储。什么是方便的格式?例如,你可能想考虑熊猫。当然,这取决于文本文件的结构。您是如何导出它的?如果您有一个原生mathematica格式(逗号和大括号分隔的Id建议首先选择一个更友好的格式),它将以您提到的原生格式存储。方便的格式是什么?