好吧,首先,大家好:)我正在运行Python 3.2 x86 @ windows
header: 256 bytes maxZ: 4 bytes (1 32-bit word) Zvalues: 2048 bytes (512 32-bit words) maxX: *same as maxZ* Xvalues: *same as Zvalues* maxY: *same as maxZ* Yvalues: *same as Zvalues*
我知道(由于格式规格),这些数字被存储为32位整数,写成2的补码
说我不需要阅读标题,这是我做的(我有1539个4字节的样本可读):
import struct file = open('C:/02.adb', 'rb') file.seek(256) fmt = '<i' nbytes = 4 for i in range(1539): packed_data = file.read(nbytes) unpacked_data = struct.unpack(fmt, packed_data) print('Sample: ',i,'Value: ',unpacked_data[0]) file.close()
读取文件并将其内容打印到标准输出,但标志有问题。 我想我不会读数字作为2的补充在这里…我如何改善脚本?
tnx的注意
另外(23.10.12):
我意识到通过编写一个更大的外部脚本的简短版本,我已经解决了主要问题 – 我自己的盲目性。
可以从文件头中读取的采样数不是确切的存储样本总数,而是除了存储在每个通道的数据块的第一个字中的最大幅度的ZXY值组合的数量。
这里是脚本的最后一个简短版本,它的输出是(和是的,签名ints的python表示是'2的补码'):
import struct file = open('C:/02.adb', 'rb') file.seek(256) transfer_ratio = 31446.540881 fmt = '<i' nbytes = 4 samples = [] for i in range(1539): packed_data = file.read(nbytes) unpacked_data = struct.unpack(fmt, packed_data) samples.append(int(unpacked_data[0])/transfer_ratio) for i in range(32): print('Ch1_val: ',samples[i+1],' Ch2_val: ',samples[i+1+513],' Ch3_val: ',samples[i+1+(513*2)]) file.close()
输出:
Ch1_val: 42.20127119933322 Ch2_val: 36.47237399942374 Ch3_val: -3.4996535999447054 Ch1_val: 41.37017819934635 Ch2_val: 36.64441199942102 Ch3_val: -4.35822179993114 Ch1_val: 42.43585979932951 Ch2_val: 34.9874729994472 Ch3_val: -4.125827399934812 Ch1_val: 44.08278179930349 Ch2_val: 34.42912859945602 Ch3_val: -4.1604257999342655 Ch1_val: 44.50289159929685 Ch2_val: 35.507053199438985 Ch3_val: -3.092836199951133 Ch1_val: 43.69110119930968 Ch2_val: 35.53335179943857 Ch3_val: -2.613991799958699 Ch1_val: 42.73662419932476 Ch2_val: 34.1708807994601 Ch3_val: -5.254886399916972 Ch1_val: 42.14097839933417 Ch2_val: 34.08460739946146 Ch3_val: -8.582724599864394 Ch1_val: 41.982105599336684 Ch2_val: 35.58544019943775 Ch3_val: -8.84688719986022 Ch1_val: 42.503816399328436 Ch2_val: 36.06170879943022 Ch3_val: -6.777056999892922 Ch1_val: 43.17568679931782 Ch2_val: 35.291926199442386 Ch3_val: -4.984618199921243 Ch1_val: 42.4303583993296 Ch2_val: 36.03750899943061 Ch3_val: -4.502275799928864 Ch1_val: 39.92264219936922 Ch2_val: 38.354075399394006 Ch3_val: -6.050744999904398 Ch1_val: 37.65501599940505 Ch2_val: 38.48181599939198 Ch3_val: -9.010943399857627 Ch1_val: 37.04642759941466 Ch2_val: 35.152642199444585 Ch3_val: -9.89107199984372 Ch1_val: 36.571876199422164 Ch2_val: 32.21988719949093 Ch3_val: -7.850465999875962 Ch1_val: 34.488403799455085 Ch2_val: 32.62164839948458 Ch3_val: -7.712485799878142 Ch1_val: 32.00259779949436 Ch2_val: 34.05570119946192 Ch3_val: -11.215064999822802 Ch1_val: 31.132422599508107 Ch2_val: 33.44755799947153 Ch3_val: -12.461179799803112 Ch1_val: 31.245853199506314 Ch2_val: 31.730771399498654 Ch3_val: -8.723057999862176 Ch1_val: 30.40820939951955 Ch2_val: 31.03355639950967 Ch3_val: -6.433076399898357 Ch1_val: 28.474165199550107 Ch2_val: 30.714824999514704 Ch3_val: -9.730672799846255 Ch1_val: 26.481449999581592 Ch2_val: 29.53720739953331 Ch3_val: -13.227496199791005 Ch1_val: 24.79611359960822 Ch2_val: 28.60448159954805 Ch3_val: -12.1075955998087 Ch1_val: 23.74938479962476 Ch2_val: 28.95253259954255 Ch3_val: -9.411305399851301 Ch1_val: 23.92663799962196 Ch2_val: 29.24305739953796 Ch3_val: -8.856490799860067 Ch1_val: 24.37285559961491 Ch2_val: 28.72398599954616 Ch3_val: -9.611549999848137 Ch1_val: 23.155774199634138 Ch2_val: 29.22521759953824 Ch3_val: -10.701304199830918 Ch1_val: 20.822671799671003 Ch2_val: 31.46641799950283 Ch3_val: -12.57088979980138 Ch1_val: 20.15302739968158 Ch2_val: 32.95071479947938 Ch3_val: -13.581175799785417 Ch1_val: 21.440195999661245 Ch2_val: 32.28628559948987 Ch3_val: -11.66643419981567 Ch1_val: 21.51502139966006 Ch2_val: 31.7910959994977 Ch3_val: -8.843802599860268
我相当肯定Python使用二进制补码来表示有符号整数。 一个常见的问题是字节码 。 规范应该提到,如果这些数字是小端或大端。 在Python中,您可以同时载入<i
和>i
。