好吧,首先,大家好:) 我運行的Python 3.2的x86 @窗口讀2的從二進制文件的補充(32位int)
所以,我已經有了一個二進制文件(6 412字節)具有如下結構:
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的關注
另外(12年10月23日):
我意識到,通過編寫一個更大的外部腳本的短版我剛剛解決的主要問題本身 - 我自己mindlessness。
可以從文件頭中讀取的樣本數並不是確切的存儲樣本總數,而是排除存儲在每個通道數據的第一個字中的最大幅度的ZXY值組合數塊。
這裏的腳本的最後短版,它的輸出(是的,簽署的int蟒蛇表示是「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
輸出是什麼? – MSN
@MSN tnx,我猜這個問題現在已經解決了,我把輸出添加到了我原來的帖子的主體中。 –