将服装二进制数据文件读入pandas数据帧

问题描述 投票:1回答:2

压力扫描器输出具有以下格式的二进制数据文件

剪掉二进制文件格式的用户手册:

enter image description here

我喜欢创建一个pandas数据框,其中所有字段都在不同的列中,温度和压力数组在单独的列中解压缩。

我对阅读服装二进制文件不太熟悉,可以使用一些帮助来启动。

最好的问候,Jan

python pandas binary
2个回答
0
投票

这里有一个显示如何做的解决方案。我只是只阅读一条记录而我没有实现所有列(长),只有一些。您只需要添加缺少的列。所以我在不同的列中爆炸了温度和压力,只需读取循环。

最后我使用库bitstring,这真的简化了二进制读取,这里我使用大端,因为文件格式是这种格式

一些读数bitstring

import pandas as pd
from  bitstring import ConstBitStream

#creating columns names
columnNames = ['pType', 'pSize', 'fNumber', 'scT', 'unitconv', 'exTrigger']
for i in range(8):
    columnNames.append('Temp' + str(i+1))
for i in range(64):
    columnNames.append('Pressure' + str(i+1))

columnNames.append('fTime_sec');columnNames.append('fTime_nano')

df = pd.DataFrame(columns=columnNames)
print(df)

s = ConstBitStream(filename='e:\\+poub\\test.pdf')

index = 0
#you could do a loop if more records in file
#read file bytes and put in the equivalent column of dataframe
df.at[index, 'pType']=  s.read(4 * 8).intbe      #read 4 bytes int big endian
df.at[index, 'pSize'] = s.read(4 * 8).intbe      #again
df.at[index, 'fNumber'] = s.read(4 * 8).intbe
df.at[index, 'scT'] = s.read(4 * 8).intbe
df.at[index, 'unitconv'] = s.read(4 * 8).intbe
df.at[index, 'exTrigger'] = s.read(4 * 8).uintbe #read uint big endian
#read 8 temp of 4 bytes in floatb be
for i in range(8):   
    df.at[index, 'Temp' + str(i+1)] = s.read(4 * 8).floatbe
#read 64 pressure of 4 bytes in floatbe
for i in range(64):
    df.at[index, 'Pressure' + str(i+1)]= s.read(4 * 8).floatbe
df.at[index, 'fTime_sec'] = s.read(4 * 8).intbe
df.at[index, 'fTime_nano'] = s.read(4 * 8).intbe

print(df)

0
投票

Frenchy提出的方法有效,但是我们对它的记录数量不够快。最终的代码要快得多:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Create a data type with the binary data format and desired column names
dataType = [
      ('pType', 'i4'),
      ('pSize', 'i4'),
      ('frNumber','i4'),
      ('scType', 'i4'),
      ('frRate', 'f4'),
      ('vStatus', 'i4'),
      ('unitsIndex', 'i4'),
      ('unitConvFactor', 'i4'),
      ('ptpScanStart_sec', 'i4'),
      ('ptpScanStart_nsec', 'i4'),
      ('exTrigger_usec', 'u4')
      ]
# add the 8 temperatures to datatype
for i in range(8):
    dataType.append(('T'+str(i), 'f4'))
# add the 64  pressures to datatype
for i in range(64):
    dataType.append(('P'+str(i), 'f4'))
# append last items to data type
dataType.append(('frTime_sec', 'i4'))
dataType.append(('frTime_nsec', 'i4'))
dataType.append(('exTrigger_sec', 'i4'))
dataType.append(('exTrigger_nsec', 'i4'))    

dt= np.dtype(dataType)

# read data 
pd.DataFrame.from_records(np.fromfile('./data/700hz_180sec.dat', dtype=dt))
© www.soinside.com 2019 - 2024. All rights reserved.