![](/img/trans.png)
[英]Converting bytes read from a file using numpy fromfile to unicode in Python 3
[英]How to skip bytes after reading data using numpy fromfile
我正在嘗試使用numpy fromfile函數從Python中的二進制文件讀取不連續的字段。 它基於使用fread的Matlab代碼:
fseek(file, 0, 'bof');
q = fread(file, inf, 'float32', 8);
8表示讀取每個值后要跳過的字節數。 我想知道fromfile中是否有類似的選項,或者是否存在另一種從Python二進制文件中讀取特定值的方法。 謝謝你的幫助。
亨里克
像這樣的東西應該可以工作,未經測試:
import struct
floats = []
with open(filename, 'rb') as f:
while True:
buff = f.read(4) # 'f' is 4-bytes wide
if len(buff) < 4: break
x = struct.unpack('f', buff)[0] # Convert buffer to float (get from returned tuple)
floats.append(x) # Add float to list (for example)
f.seek(8, 1) # The second arg 1 specifies relative offset
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.