numpy保存字典数据,读取数据,以及读取文件目录下的所有文件(此处以npy文件为例)

这篇博客介绍了如何使用numpy保存字典和非结构化数据,重点在于利用save()和savez()函数。同时,展示了如何读取一个目录下的所有npy文件并将它们链接成一个字典进行保存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

numpy保存数组,字典,非结构话数据

注意 保存字典数组是用save()函数保存
保存非结构化数据使用savez()函数保存
非结构化读取的数据,若调用具体元素需要在后边加[()]想去请看一下例子。

import numpy as np


# 简单的数组保存
arr = np.array([[1, 2], [3, 4]])
np.save('/Users/lilong/Desktop/mm.npy',arr)
arr_load=np.load('/Users/lilong/Desktop/mm.npy')
print(arr_load,'\n\n',arr_load[0])

# 保存字典
dict_ = {'a' : 1, 'b': 2}
np.save('/Users/lilong/Desktop/dd.npy',arr)
dict_load=np.load('/Users/lilong/Desktop/dd.npy')
print('\n\n',dict_load,'\n\n',dict_load['a'])


# 非结构化数据保存
str_ = 'abc'
arr_ = np.array([[1, 2], [3, 4]])
dict_ = {'a' : 1, 'b': 2}
np.savez('/Users/lilong/Desktop/nn.npz', st= str_, ar = arr_, dic= dict_)


data = np.load('/Users/lilong/Desktop/nn.npz')
print('....\n',data['st'],'\n',data['st'][()])
print('....\n',data['ar'],'....\n',data['ar'][0],'\n\n',data['ar'][()][0])
print('....\n',data['dic'],'\n\n',data['dic'][()])
#print('....\n',data['dic']['a'])  # 报错
print('....\n',data['dic'][()]['a'])

输出结果:

[[1 2]
 [3 4]] 

 [1 2]

 {'a': 1, 'b': 2} 
 
1
....
 abc 
 abc
....
 [[1 2]
 [3 4]] ....
 [1 2] 

 [1 2]
....
 {'a': 1, 'b': 2} 

 {'a': 1, 'b': 2}
....
 1

读取一个目录下的所有npy文件,并链接成一个字典并保存

import os
import numpy as np
import matplotlib.pyplot as plt

# /home/kuka_data/256x256 下共有80个npy文件
# 每个文件中有82条数据,每条数据是一个字典,其中有两个元素
# Disp 中存的是一个一行三列矩阵
#  obs 中存的是一张256x256x4的彩色图片矩阵(numpy)
dict_data = {"Disp": [], "obs": []}
for path, lists, frame in os.walk("/home/kuka_data/256x256"):
    print(path)
    print(lists)
    print(frame)

    for value in frame:
        # print(value)
        data = np.load(path+"/"+value,allow_pickle=True).item()
        # print(data)
        for key,values in data.items():
            dict_data[key].extend(values)

np.save('/home/kuka_data/data', dict_data)

print(len(dict_data["Disp"]))
print(np.array(dict_data["Disp"]).shape)
print(dict_data["obs"][1].shape)

load_dict = np.load('/home/kuka_data/data.npy',allow_pickle=True).item()
print(len(load_dict["Disp"]))
print(np.array(load_dict["Disp"]).shape)
print(load_dict["obs"][1].shape)

结果

/home/kuka_data/256x256
[]
['kuku_displacement2-1566648687.481698.npy', 'kuku_displacement2-1566648836.7948618.npy', 'kuku_displacement2-1566648802.7720268.npy', 'kuku_displacement2-1566648829.7204263.npy', 'kuku_displacement2-1566648738.5510986.npy', 'kuku_displacement2-1566648795.8393836.npy', 'kuku_displacement2-1566648679.6099687.npy', 'kuku_displacement2-1566648578.9564948.npy', 'kuku_displacement2-1566648607.8536177.npy', 'kuku_displacement2-1566648600.5029716.npy', 'kuku_displacement2-1566648745.6686969.npy', 'kuku_displacement2-1566648717.2477114.npy', 'kuku_displacement2-1566648781.556605.npy', 'kuku_displacement2-1566648571.9120362.npy', 'kuku_displacement2-1566648815.408891.npy', 'kuku_displacement2-1566648774.5511177.npy', 'kuku_displacement2-1566648831.351188.npy', 'kuku_displacement2-1566648752.824365.npy', 'kuku_displacement2-1566648824.4033365.npy', 'kuku_displacement2-1566648758.49715.npy', 'kuku_displacement2-1566648686.7624846.npy', 'kuku_displacement2-1566648586.2092843.npy', 'kuku_displacement2-1566648810.2000067.npy', 'kuku_displacement2-1566648593.1538253.npy', 'kuku_displacement2-1566648636.5948677.npy', 'kuku_displacement2-1566648643.9855971.npy', 'kuku_displacement2-1566648788.6289644.npy', 'kuku_displacement2-1566648767.5792081.npy', 'kuku_displacement2-1566648702.697138.npy', 'kuku_displacement2-1566648715.7133927.npy', 'kuku_displacement2-1566648643.735781.npy', 'kuku_displacement2-1566648729.729601.npy', 'kuku_displacement2-1566648607.585559.npy', 'kuku_displacement2-1566648593.1402547.npy', 'kuku_displacement2-1566648701.5483274.npy', 'kuku_displacement2-1566648693.7564404.npy', 'kuku_displacement2-1566648731.4295425.npy', 'kuku_displacement2-1566648838.2915816.npy', 'kuku_displacement2-1566648680.0441225.npy', 'kuku_displacement2-1566648760.147938.npy', 'kuku_displacement2-1566648579.021789.npy', 'kuku_displacement2-1566648708.7174547.npy', 'kuku_displacement2-1566648622.2831016.npy', 'kuku_displacement2-1566648800.9260888.npy', 'kuku_displacement2-1566648807.9214253.npy', 'kuku_displacement2-1566648794.0541906.npy', 'kuku_displacement2-1566648772.8288097.npy', 'kuku_displacement2-1566648614.833823.npy', 'kuku_displacement2-1566648586.2228277.npy', 'kuku_displacement2-1566648722.7077215.npy', 'kuku_displacement2-1566648672.4963348.npy', 'kuku_displacement2-1566648694.6013956.npy', 'kuku_displacement2-1566648779.953965.npy', 'kuku_displacement2-1566648765.708791.npy', 'kuku_displacement2-1566648744.098226.npy', 'kuku_displacement2-1566648672.1221662.npy', 'kuku_displacement2-1566648600.7277622.npy', 'kuku_displacement2-1566648658.0078316.npy', 'kuku_displacement2-1566648651.0598576.npy', 'kuku_displacement2-1566648787.0580876.npy', 'kuku_displacement2-1566648709.9425685.npy', 'kuku_displacement2-1566648817.3893623.npy', 'kuku_displacement2-1566648665.199489.npy', 'kuku_displacement2-1566648629.2857726.npy', 'kuku_displacement2-1566648651.0284786.npy', 'kuku_displacement2-1566648822.6401062.npy', 'kuku_displacement2-1566648614.9254959.npy', 'kuku_displacement2-1566648557.7999866.npy', 'kuku_displacement2-1566648564.842408.npy', 'kuku_displacement2-1566648564.8106415.npy', 'kuku_displacement2-1566648622.0170007.npy', 'kuku_displacement2-1566648629.633209.npy', 'kuku_displacement2-1566648571.8874032.npy', 'kuku_displacement2-1566648665.0909448.npy', 'kuku_displacement2-1566648636.7024834.npy', 'kuku_displacement2-1566648557.730382.npy', 'kuku_displacement2-1566648751.2538974.npy', 'kuku_displacement2-1566648737.0951996.npy', 'kuku_displacement2-1566648724.288052.npy', 'kuku_displacement2-1566648658.1410203.npy']
6560
(6560, 3)
(256, 256, 4)

6560
(6560, 3)
(256, 256, 4)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值