python获取文件字节数,从Python中的文件中提取所需的字节

本文介绍了如何使用Python从文件中提取特定字节数据,针对给定的偏移量、数据类型和总字节数,通过numpy库进行操作。在遇到字节序不匹配的问题时,提供了如何处理字节序的解决方案。
摘要由CSDN通过智能技术生成

I have to extract the following data from that file:

Byte Offset: 176

Data type: 4-byte (long) integer

Total bytes: 160

I tried as follows:

import numpy as np

fname = 'GLA06_634_1102_001_0079_3_01_0001.DAT'

with open(fname,'rb') as fi:

fi.seek (176,0)

data= np.fromfile(fi,dtype='long',count=160)

print data

No success, what's wrong with my idea?

解决方案

Using a hard coded offset is a rather fragile solution. But assuming you know what you are doing:

Byte Offset: 176

Data type: 4-byte (long) integer

Total bytes: 160

AKAICT, that leads to 160/4 = 40 values to read (could you confirm that?)

In addition, the type should be one of the numpy defined type. Here np.int32 might be the right one:

data=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值