NumPy 讀寫文件

2021-09-24 19:18 更新

此頁面處理常見應用程序,有關 I/O 例程的完整集合,請參閱輸入和輸出

讀取文件和CSV文件

沒有缺失值

使用 numpy.loadtxt

有缺失值

使用 numpy.getfromtxt

numpy.getfromtxt要么:

  • 返回一個屏蔽數(shù)組,屏蔽掉缺失值(如果usemask = True
  • 用中指定的值?填充缺失值filling_values(默認np.nan為浮點數(shù),-1 為整數(shù))。

使用非空白分隔符

  1. >>> print(open("csv.txt").read())
  2. 1, 2, 3
  3. 4,, 6
  4. 7, 8, 9

掩碼陣列輸出

  1. >>> np.genfromtxt("csv.txt", delimiter=",", usemask=True)
  2. masked_array(
  3. data=[[1.0, 2.0, 3.0],
  4. [4.0, --, 6.0],
  5. [7.0, 8.0, 9.0]],
  6. mask=[[False, False, False],
  7. [False, True, False],
  8. [False, False, False]],
  9. fill_value=1e+20)

數(shù)組輸出

  1. >>> np.genfromtxt("csv.txt", delimiter=",")
  2. array([[ 1., 2., 3.],
  3. [ 4., nan, 6.],
  4. [ 7., 8., 9.]])

數(shù)組輸出,指定填充值

  1. >>> np.genfromtxt("csv.txt", delimiter=",", dtype=np.int8, filling_values=99)
  2. array([[ 1, 2, 3],
  3. [ 4, 99, 6],
  4. [ 7, 8, 9]], dtype=int8)

空格分隔

numpy.genfromtxt?還可以解析具有缺失值的以空格分隔的數(shù)據(jù)文件,如果 每個字段都有一個固定的寬度:使用寬度作為分隔符參數(shù)。

  1. ## File with width=4. The data does not have to be justified (for example,
  2. ## the 2 in row 1), the last column can be less than width (for example, the 6
  3. ## in row 2), and no delimiting character is required (for instance 8888 and 9
  4. ## in row 3)
  5. >>> f = open("fixedwidth.txt").read() # doctest: +SKIP
  6. >>> print(f) # doctest: +SKIP
  7. 1 2 3
  8. 44 6
  9. 7 88889
  10. ## Showing spaces as ^
  11. >>> print(f.replace(" ","^")) # doctest: +SKIP
  12. 1^^^2^^^^^^3
  13. 44^^^^^^6
  14. 7^^^88889
  15. >>> np.genfromtxt("fixedwidth.txt", delimiter=4) # doctest: +SKIP
  16. array([[1.000e+00, 2.000e+00, 3.000e+00],
  17. [4.400e+01, nan, 6.000e+00],
  18. [7.000e+00, 8.888e+03, 9.000e+00]])

特殊值(例如“x”)表示缺少字段:將其用作?missing_values參數(shù)。

  1. >>> print(open("nan.txt").read())
  2. 1 2 3
  3. 44 x 6
  4. 7 8888 9
  5. >>> np.genfromtxt("nan.txt", missing_values="x")
  6. array([[1.000e+00, 2.000e+00, 3.000e+00],
  7. [4.400e+01, nan, 6.000e+00],
  8. [7.000e+00, 8.888e+03, 9.000e+00]])

您想跳過缺少值的行:設置?invalid_raise=False。

  1. >>> print(open("skip.txt").read())
  2. 1 2 3
  3. 44 6
  4. 7 888 9
  5. >>> np.genfromtxt("skip.txt", invalid_raise=False)
  6. __main__:1: ConversionWarning: Some errors were detected !
  7. Line #2 (got 2 columns instead of 3)
  8. array([[ 1., 2., 3.],
  9. [ 7., 888., 9.]])

分隔符空白字符不同于表示丟失數(shù)據(jù)的空白字符。例如,如果列由 分隔\t,那么如果缺失數(shù)據(jù)由一個或多個空格組成,則將被識別。

  1. >>> f = open("tabs.txt").read()
  2. >>> print(f)
  3. 1 2 3
  4. 44 6
  5. 7 888 9
  6. ## Tabs vs. spaces
  7. >>> print(f.replace("\t","^"))
  8. 1^2^3
  9. 44^ ^6
  10. 7^888^9
  11. >>> np.genfromtxt("tabs.txt", delimiter="\t", missing_values=" +")
  12. array([[ 1., 2., 3.],
  13. [ 44., nan, 6.],
  14. [ 7., 888., 9.]])

讀取 .npy 或 .npz 格式的文件

選擇:

  • 使用numpy.load.?它可以讀取任何的生成的文件?numpy.savenumpy.saveznumpy.savez_compressed。
  • 使用內(nèi)存映射。見numpy.lib.format.open_memmap。

寫入文件供 NumPy 讀取

二進制

使用?numpy.save, 或 來存儲多個數(shù)組numpy.savez?或numpy.savez_compressed

為了安全性和可移植性,allow_pickle=False除非 dtype 包含需要酸洗的 Python 對象,否則設置?。

掩碼數(shù)組,其他任意數(shù)組子類也不能。can't?currently?be?saved

人類可讀

numpy.savenumpy.savez創(chuàng)建二進制文件。要編寫人類可讀的文件,請使用numpy.savetxt.?該數(shù)組只能是一維或二維的,并且沒有用于多個文件的 savetxtz。

大型數(shù)組

請參閱寫入或讀取大型數(shù)組。

讀取任意格式的二進制文件("binary blob")

使用結構化數(shù)組 例子: 該.wav文件頭是前面的44字節(jié)塊data_size的實際聲音數(shù)據(jù)的字節(jié)數(shù):

  1. chunk_id "RIFF"
  2. chunk_size 4-byte unsigned little-endian integer
  3. format "WAVE"
  4. fmt_id "fmt "
  5. fmt_size 4-byte unsigned little-endian integer
  6. audio_fmt 2-byte unsigned little-endian integer
  7. num_channels 2-byte unsigned little-endian integer
  8. sample_rate 4-byte unsigned little-endian integer
  9. byte_rate 4-byte unsigned little-endian integer
  10. block_align 2-byte unsigned little-endian integer
  11. bits_per_sample 2-byte unsigned little-endian integer
  12. data_id "data"
  13. data_size 4-byte unsigned little-endian integer

.wav作為 NumPy 結構化 dtype的文件頭:

  1. wav_header_dtype = np.dtype([
  2. ("chunk_id", (bytes, 4)), # flexible-sized scalar type, item size 4
  3. ("chunk_size", "<u4"), # little-endian unsigned 32-bit integer
  4. ("format", "S4"), # 4-byte string, alternate spelling of (bytes, 4)
  5. ("fmt_id", "S4"),
  6. ("fmt_size", "<u4"),
  7. ("audio_fmt", "<u2"), #
  8. ("num_channels", "<u2"), # .. more of the same ...
  9. ("sample_rate", "<u4"), #
  10. ("byte_rate", "<u4"),
  11. ("block_align", "<u2"),
  12. ("bits_per_sample", "<u2"),
  13. ("data_id", "S4"),
  14. ("data_size", "<u4"),
  15. #
  16. # the sound data itself cannot be represented here:
  17. # it does not have a fixed size
  18. ])
  19. header = np.fromfile(f, dtype=wave_header_dtype, count=1)[0]

這個.wav例子是為了說明;要.wav在現(xiàn)實生活中讀取文件,請使用 Python 的內(nèi)置模塊wave。

寫入或讀取大型數(shù)組

太大而無法放入內(nèi)存的數(shù)組可以像使用內(nèi)存映射的普通內(nèi)存中數(shù)組一樣處理。

  • 原始數(shù)組數(shù)據(jù)寫入numpy.ndarray.tofile或?numpy.ndarray.tobytes可以讀取numpy.memmap
    1. array = numpy.memmap("mydata/myarray.arr", mode="r", dtype=np.int16, shape=(1024, 1024))
  • 通過輸出文件numpy.save(即,使用numpy的格式)可以使用讀取numpy.load與所述mmap_mode關鍵字參數(shù):
    1. large_array[some_slice] = np.load("path/to/small_array", mmap_mode="r")

內(nèi)存映射缺乏數(shù)據(jù)分塊和壓縮等功能;可與 NumPy 一起使用的更多功能齊全的格式和庫包括:

  • HDF5:h5py或PyTables。
  • 扎爾:這里。
  • NetCDF?:?scipy.io.netcdf_file.

有關 memmap、Zarr 和 HDF5 之間的權衡,請參閱?pythonspeed.com。

寫入文件供其他(非 NumPy)工具讀取

與其他工具交換數(shù)據(jù)的格式包括 HDF5、Zarr 和 NetCDF(請參閱寫入或讀取大型數(shù)組)。

寫入或讀取 JSON 文件

NumPy 數(shù)組不能直接?JSON 序列化。

使用 pickle 文件保存/恢復

盡可能避免;泡菜對于錯誤或惡意構建的數(shù)據(jù)并不安全。

使用numpy.savenumpy.load。Set?allow_pickle=False,除非數(shù)組 dtype 包含 Python 對象,在這種情況下需要酸洗。

從 Pandas DataFrame 轉換為 NumPy 數(shù)組

pandas.DataFrame.to_numpy

使用tofile和保存/恢復fromfile

一般來說,更喜歡numpy.savenumpy.load。

numpy.ndarray.tofilenumpy.fromfile丟失有關字節(jié)順序和精度的信息,因此不適用于除臨時存儲之外的任何其他內(nèi)容。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號