python - 读取 .npy 文件时检索错误
问题描述
我正在尝试读取一个大的 .npy 文件,但我无法读取该文件。下面是我用于读取文件的 python 代码。
import numpy as np
pre_train = np.load('weights.npy',allow_pickle=True, encoding="latin1")
data_pic = pre_train.item()
#print(type(data_dic))
for item in data_pic:
print(item)
错误:data_pic = pre_train.item()
Can only convert an array of size 1 to a Python scalar
解决方案
加载文件时您的代码不会崩溃。使用numpy.ndarray.item时崩溃。在您的情况下,您不需要使用item()
.
使用一个好的旧 for 循环就可以了!
data = np.load('...')
for i in data:
for j in i:
print(j)
# 2, 2, 6, 1, ...
推荐阅读
- python - JSON 文件:使用 Python 对不同对象进行单独的字数统计
- ruby - 弃用:HTTParty 将不再覆盖 `response#nil?`。这个弃用警告是什么意思?
- javascript - 更新类 javascript 中函数的结果
- javascript - TypeORM 自定义存储库,从指定列中选择不同的值
- php - Array_combine 错误:输出错误的值并仅给出唯一值 php
- python - 将条件格式应用于excel中的整行
- linux - 从linux中的top命令计算CPU使用率
- python - 无法导入名称“类型”
- snapshot - 快照中的 VMWare 虚拟磁盘文件 (.vmdk) 堆积,无法在不破坏 VM 的情况下删除
- java - 如何在android中显示警报对话框