【问题标题】:Reading a costume binary data file into a pandas data frame将服装二进制数据文件读入熊猫数据帧
【发布时间】:2019-03-25 19:12:09
【问题描述】:

压力扫描仪输出如下格式的二进制数据文件

截取二进制文件格式的用户手册:

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

我对阅读服装二进制文件不是很熟悉,可以借助一些帮助开始。

最好的问候, 一月

【问题讨论】:

  • 您能以文本形式发布您的文件格式吗?
  • 另外一个问题,可以存在一条记录还是多条相同类型的记录?
  • 看模块struct

标签: python pandas binary


【解决方案1】:

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

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

一些读数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)

【讨论】:

  • 嗨,Frenchy,谢谢这对我有很大帮助。最好的问候,简
  • 如果答案正常,请不要忘记验证它
  • 验证需要一段时间 我们的风洞中的实验装置正处于准备阶段 实际数据尚不可用。我将使用一组虚拟数据对其进行测试。
  • 验证成功??
  • 您好 Frenchy,验证没问题。非常感谢您的帮助。
【解决方案2】:

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))

【讨论】:

    猜你喜欢
    • 2021-07-28
    • 2012-09-28
    • 2016-09-21
    • 1970-01-01
    • 2021-02-06
    • 2017-05-20
    • 2022-12-21
    • 2021-08-05
    相关资源
    最近更新 更多