python numpy 常用文件格式 导出与载入(npy,npz,txt,csv,xls,xlsx,ods)

今天单介绍下用nunpy进行文件格式的保存与导入。
*.npy  *.npz   *.txt  *.csv   *.xlsx   *.xls   *.ods
下一期,介绍如何在c++中读取numpy保存的npy npz数据
主要是因为在ros中使用c++的脚本效率会比python高。便于做机器人相关的控制。


1 , npy将数组的二进制格式保存;
2 , npz是多个npy的压缩包形式;运行后生成的文件,我们看看,还是npy。

3 , txt这个就不解释了
4 , csv实际上仍旧是txt,格式稍有区别

5 , windos下的excel格式

介绍两种读取方式1)修改后缀为csv
2) pip3 install xlrd==1.2.0 直接安装xlrd会安装最新的,仅支持xls,不支持xlsxX。xls是excel2003及以前版本生成的文件格式,而xlsx是excel2007及以后版本生成的文件格式。xls二进制;xlsx是基于XML结构;
6 , ods ubuntu自带的类似excel的软件格式。
pyexcel-ods

*****************************************************

import numpy as np
arr = np.arange(5)  # 生成数组
np.save('test',arr)  # 保存下来
print(np.load('test.npy'))  # np.load() 载入

# 生成数组
a = np.arange(4)
b = np.arange(5)
c = np.arange(6)

np.savez('test', a, b, c_array = c)
data = np.load('test.npz')
{'arr_0':a, 'arr_1':b, 'c_array':c}

print('arr_0 :', data['arr_0'])
print('arr_1 :', data['arr_1'])
print('c_array :', data['c_array'])

d = np.arange(10)
np.savetxt('out.txt', d)
print(np.loadtxt('out.txt'))

"""
[0 1 2 3 4]
arr_0 : [0 1 2 3]
arr_1 : [0 1 2 3 4]
c_array : [0 1 2 3 4 5]
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
"""

官网的代码示例:pyexcel-ods · PyPI



 

你可能感兴趣的:(Python,python)