2013-07-17 62 views
2

我有一个二进制文件,我可以在MATLAB中打开,但无法用Python打开。二进制文件编码为“双浮”,从而通过MATLAB与下面一行写着:等效Matlab的fread()'float64'在Python中

fread(fopen(fileName), 'float64'); 

在Python,我真的不知道如何复制这一行。我认为使用Numpy会是一个很好的开始,所以我尝试了以下几行,但没有得到我期望的结果。每行有6个数字,我只有第一个和一个'NaN'。

from numpy import * 
f = open('filename', 'rb') 
a = fromfile(f, double64, 10) 
print a 

对此的任何帮助将非常赞赏;我已经在下面的评论中发布了二进制文件和MATLAB解析文件。我不需要专门使用Numpy,我可以使用任何基于Python的解决方案。谢谢。

+1

“每行有6个数字” - “行”?他们是ASCII编码还是IEEE-float编码? –

+1

链接为数据文件:https://www.dropbox.com/s/2mggkyyzlpcrs31/TEMPO3.2F-0215_s00116.dat – Anish

+1

链接为MATLAB解析文件:https://www.dropbox.com/s/nk8mm40uovgeixu/ TEMPO3.2F-0215_s00116.csv – Anish

回答

6

每一秒值nan所以这可能是一些分隔符。此外,该文件中的值是列优先的。下面的脚本在数据读取操作,扔掉的NaN的条目,操纵阵列成正确的形状,并且输出一个CSV文件,该文件是相同的您发布的一个:

import csv 
import numpy as np 

# Pull in all the raw data. 
with open('TEMPO3.2F-0215_s00116.dat', 'rb') as f: 
    raw = np.fromfile(f, np.float64) 

# Throw away the nan entries. 
raw = raw[1::2] 

# Check its a multiple of six so we can reshape it. 
if raw.size % 6: 
    raise ValueError("Data size not multiple of six.") 

# Reshape and take the transpose to manipulate it into the 
# same shape as your CSV. The conversion to integer is also 
# so the CSV file is the same. 
data = raw.reshape((6, raw.size/6)).T.astype('int') 

# Dump it out to a CSV. 
with open('test.csv', 'w') as f: 
    w = csv.writer(f) 
    w.writerows(data) 

编辑:已更新版本与jorgeca建议的更改:

import csv 
import numpy as np 

# Pull in all the raw data. 
raw = np.fromfile('TEMPO3.2F-0215_s00116.dat', np.float64) 

# Throw away the nan entries. 
raw = raw[1::2] 

# Reshape and take the transpose to manipulate it into the 
# same shape as your CSV. The conversion to integer is also 
# so the CSV file is the same. 
data = raw.reshape((6, -1)).T.astype('int') 

# Dump it out to a CSV. 
with open('test.csv', 'w') as f: 
    w = csv.writer(f) 
    w.writerows(data) 
+0

为了让任何维护你的代码的人感到困惑,你总是可以读取数据为'complex128',然后调用'np.imag() “从虚构部分取数值... – Blair

+0

...并且不,这并不是一个严肃的建议:P。 – Blair

+1

好的答案!你可能知道一些评论,但我觉得很有趣:'np.fromfile'接受一个文件名,所以你不必将它包装在'with ...'块中; '.reshape'如果无法执行,则会产生ValueError,所以本着[EAFP](http://docs.python.org/2/glossary.html#term-eafp)的精神,可以删除错误检查(反正它会失败); '.reshape'接受-1作为“通配符”维度。 – jorgeca

4

有一个在MATLAB生产上读交替的数据,并且NaN,例如您的数据值之间的分隔符:

NaN 
2134 
NaN 
2129 
NaN 
2128 
.... 
1678 

与numpy的:

[ nan 2134. nan ..., 1681. nan 1678.] 

我用得到相同的输入你用Matlab或numpy发布的代码(1.7)。请注意,根据csv文件中的模式,数据是按列方式从dat文件读取的,而不是按行方式读取的。

让所有在numpy的数据的尝试

a = fromfile(file=f, dtype=float64, count=-1)