【讀取檔案】python使用numpy模組讀取檔案
阿新 • • 發佈:2020-12-08
技術標籤:讀取檔案numpy.fromfile
函式原型
numpy.fromfile(file, dtype=float, count=-1, sep=’’, offset=0)
主要以二進位制格式讀取說明
引數
file: 檔案路徑
dtype: 資料型別,np.uint8、np.float32等支援的資料型別
其餘預設即可
【sample】
讀取硬碟上一個檔案C:\dat,檔案屬性如下
檔案開頭部分內容十六進位制表示如下:
In [1]: import numpy as np
In [2]: dat = np.fromfile("C:\\dat", dtype= np.uint8)
In [3]: dat.shape
Out[3]: (496554,) # ==> 大小正好等於檔案位元組數
# 以十六進位制輸出前10個位元組
In [4]: for i in range(10):
print("0x%02X " % dat[i]) # %02X表示以十六進位制輸出,右對齊,表示為兩位數字,不足兩位用0填充(此為字串格式化輸出相關知識,可參照相關部分)
Out[4]: 0x42
0x4D
0xAA
0x93
0x07
0x00
0x00
0x00
0x00
0x00