numpy中文件的存储和读取-嵩天老师笔记
numpy中csv文件的存储和读取
CSV文件:(Comma‐Separated Value, 逗号分隔值)
一维和二维数组
存储
np.savetxt(frame,array,fmt='%.18e',delimiter=None,newline='\n', header='', footer='', comments='# ', encoding=None)
- frame : 文件、字符串或产生器,可以是.gz或.bz2的压缩文件 。
- array : 存入文件的数组 (一维或者二维)。
- fmt:写入文件的格式,例如: %d %.2f %.18e 。
- delimiter : 分割字符串,默认是任何空格 。
其他参数看文档。
import numpy as np
a = np.arange(100).reshape((5,20))
np.savetxt('a.csv',a,fmt = '%d',delimiter=',')
b = np.loadtxt('a.csv',delimiter=',')
b
array([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.,
        13., 14., 15., 16., 17., 18., 19.],
       [20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32.,
        33., 34., 35., 36., 37., 38., 39.],
       [40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51., 52.,
        53., 54., 55., 56., 57., 58., 59.],
       [60., 61., 62., 63., 64., 65., 66., 67., 68., 69., 70., 71., 72.,
        73., 74., 75., 76., 77., 78., 79.],
       [80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90., 91., 92.,
        93., 94., 95., 96., 97., 98., 99.]])
读取
np.loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes')
- frame : 文件、字符串或产生器,可以是.gz或.bz2的压缩文件。
- dtype : 数据类型,可选 。
- delimiter : 分割字符串,默认是任何空格 。
- usecols:选取数据的列。
- unpack : 如果True,读入属性将分别写入不同变量 。
其他看文档。
注意:usecols,如果选取前三列,应该是usecols=(0,1,2),如果只选取第三列,应该是usecols=(2,)。但是,我尝试了一下,不输入逗号也可以,usecols=(2)。
b = np.loadtxt('a.csv',dtype = np.int,delimiter=',',usecols=(0,1,2))
b
array([[ 0,  1,  2],
       [20, 21, 22],
       [40, 41, 42],
       [60, 61, 62],
       [80, 81, 82]])
b = np.loadtxt('a.csv',dtype = np.int,delimiter=',',usecols=(2,))
b
array([ 2, 22, 42, 62, 82])
b = np.loadtxt('a.csv',dtype = np.int,delimiter=',',usecols=(2))
b
array([ 2, 22, 42, 62, 82])
需要注意的是CSV文件只能有效存储一维和二维数组 。
np.savetxt() np.loadtxt()只能有效存取一维和二维数组。
多维数组(任意维度)
存储
a.tofile(frame, sep='', format='%s') 
- frame : 文件、字符串
- sep : 数据分割字符串,如果是空串,写入文件为二进制。即,默认为空串。
- format : 写入数据的格式
import numpy as np
a = np.arange(100).reshape((5,10,2))
a
Out[3]:
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],
       [[20, 21],
        [22, 23],
        [24, 25],
        [26, 27],
        [28, 29],
        [30, 31],
        [32, 33],
        [34, 35],
        [36, 37],
        [38, 39]],
       [[40, 41],
        [42, 43],
        [44, 45],
        [46, 47],
        [48, 49],
        [50, 51],
        [52, 53],
        [54, 55],
        [56, 57],
        [58, 59]],
       [[60, 61],
        [62, 63],
        [64, 65],
        [66, 67],
        [68, 69],
        [70, 71],
        [72, 73],
        [74, 75],
        [76, 77],
        [78, 79]],
       [[80, 81],
        [82, 83],
        [84, 85],
        [86, 87],
        [88, 89],
        [90, 91],
        [92, 93],
        [94, 95],
        [96, 97],
        [98, 99]]])
a.tofile('b.dat',sep=',',format='%d')
读取
np.fromfile(frame, dtype=float, count=‐1, sep='') 
- frame : 文件、字符串
- dtype : 读取的数据类型 。可以发现,我们读取数据的时候都需要指定数据类型,无论是不是一维二维。默认为浮点型
- count : 读入元素个数, ‐1表示读入整个文件
- sep : 数据分割字符串,如果是空串,写入文件为二进制
c = np.fromfile('b.dat',dtype=np.int,sep=',')
c
Out[6]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
       68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
可以发现,读取到的数据维度信息丢失了。因此,我们需要将维度信息告诉c。
c = np.fromfile('b.dat',dtype=np.int,sep=',').reshape((5,10,2))
c
Out[14]:
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],
       [[20, 21],
        [22, 23],
        [24, 25],
        [26, 27],
        [28, 29],
        [30, 31],
        [32, 33],
        [34, 35],
        [36, 37],
        [38, 39]],
       [[40, 41],
        [42, 43],
        [44, 45],
        [46, 47],
        [48, 49],
        [50, 51],
        [52, 53],
        [54, 55],
        [56, 57],
        [58, 59]],
       [[60, 61],
        [62, 63],
        [64, 65],
        [66, 67],
        [68, 69],
        [70, 71],
        [72, 73],
        [74, 75],
        [76, 77],
        [78, 79]],
       [[80, 81],
        [82, 83],
        [84, 85],
        [86, 87],
        [88, 89],
        [90, 91],
        [92, 93],
        [94, 95],
        [96, 97],
        [98, 99]]])
如果我们不指定分隔符,则读取也不需要指定,此时存储的是二进制文件。
a = np.arange(100).reshape((5,10,2))
a
Out[18]:
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],
       [[20, 21],
        [22, 23],
        [24, 25],
        [26, 27],
        [28, 29],
        [30, 31],
        [32, 33],
        [34, 35],
        [36, 37],
        [38, 39]],
       [[40, 41],
        [42, 43],
        [44, 45],
        [46, 47],
        [48, 49],
        [50, 51],
        [52, 53],
        [54, 55],
        [56, 57],
        [58, 59]],
       [[60, 61],
        [62, 63],
        [64, 65],
        [66, 67],
        [68, 69],
        [70, 71],
        [72, 73],
        [74, 75],
        [76, 77],
        [78, 79]],
       [[80, 81],
        [82, 83],
        [84, 85],
        [86, 87],
        [88, 89],
        [90, 91],
        [92, 93],
        [94, 95],
        [96, 97],
        [98, 99]]])
a.tofile('b.dat',format='%d')
c = np.fromfile('b.dat',dtype=np.int).reshape((5,10,2))
c
Out[21]:
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],
       [[20, 21],
        [22, 23],
        [24, 25],
        [26, 27],
        [28, 29],
        [30, 31],
        [32, 33],
        [34, 35],
        [36, 37],
        [38, 39]],
       [[40, 41],
        [42, 43],
        [44, 45],
        [46, 47],
        [48, 49],
        [50, 51],
        [52, 53],
        [54, 55],
        [56, 57],
        [58, 59]],
       [[60, 61],
        [62, 63],
        [64, 65],
        [66, 67],
        [68, 69],
        [70, 71],
        [72, 73],
        [74, 75],
        [76, 77],
        [78, 79]],
       [[80, 81],
        [82, 83],
        [84, 85],
        [86, 87],
        [88, 89],
        [90, 91],
        [92, 93],
        [94, 95],
        [96, 97],
        [98, 99]]])
需要注意
- 该方法需要读取时知道存入文件时数组的维度和元素类型
- a.tofile()和np.fromfile()需要配合使用
- 可以通过元数据文件来存储额外信息
Numpy文件的便捷存取
np.save(fname, array) 或 np.savez(fname, array)
- fname : 文件名,以.npy为扩展名,压缩扩展名为.npz
- array : 数组变量
np.load(fname)
- fname : 文件名,以.npy为扩展名,压缩扩展名为.npz
a = np.arange(100).reshape(5, 10, 2)
np.save('a.npy',a)
b = np.load('a.npy)
            b = np.load('a.npy')
b
Out[26]:
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],
       [[20, 21],
        [22, 23],
        [24, 25],
        [26, 27],
        [28, 29],
        [30, 31],
        [32, 33],
        [34, 35],
        [36, 37],
        [38, 39]],
       [[40, 41],
        [42, 43],
        [44, 45],
        [46, 47],
        [48, 49],
        [50, 51],
        [52, 53],
        [54, 55],
        [56, 57],
        [58, 59]],
       [[60, 61],
        [62, 63],
        [64, 65],
        [66, 67],
        [68, 69],
        [70, 71],
        [72, 73],
        [74, 75],
        [76, 77],
        [78, 79]],
       [[80, 81],
        [82, 83],
        [84, 85],
        [86, 87],
        [88, 89],
        [90, 91],
        [92, 93],
        [94, 95],
        [96, 97],
        [98, 99]]])
这里的存储,实际上也是二进制文件,如果只在python中进行操作,这种方法很方便,如果需要与其他程序进行交互,则需要视情况存储为CSV文件等。
numpy中文件的存储和读取-嵩天老师笔记的更多相关文章
- 复杂dic的文件化存储和读取问题
		今天遇到一个难题.整出一个复杂的dic,里面不仅维度多,还含有numpy.array.超级复杂.过程中希望能够存储一下,万一服务器停了呢?万一断电了呢? 结果存好存,取出来可就不是那样了.网上搜索了很 ... 
- android IO流操作文件(存储和读取)
		存储文件: public class FileOperate extends Activity { private static final String FILENAME = "mydat ... 
- iOS NSMutableDictionary中UIImage的存储和读取
		思路:将UIImage转换成NSData,然后插入到NSMutableDictionary中.读取时,用NSData读出来,然后再转换成UIImage -存储 UIImage *image = [se ... 
- Numpy用于数组数据的存储和读取
		Python的Numpy模块可用于存储和读取数据: 1.将一个数组存储为二进制文件 Numpy.save:将一个数组以.npy的格式保存为二进制文件 调用格式:numpy.save(file, arr ... 
- [转] C#实现在Sql Server中存储和读取Word文件 (Not Correct Modified)
		出处 C#实现在Sql Server中存储和读取Word文件 要实现在Sql Server中实现将文件读写Word文件,需要在要存取的表中添加Image类型的列,示例表结构为: CREATE TABL ... 
- 在IOS中使用DES算法对Sqlite数据库进行内容加密存储并读取解密
		在IOS中使用DES算法对Sqlite 数据库进行内容加密存储并读取解密 涉及知识点: 1.DES加密算法: 2.OC对Sqlite数据库的读写: 3.IOS APP文件存储的两种方式及读取方式. 以 ... 
- Android中的数据存储(二):文件存储                                                                                            2017-05-25 08:16             35人阅读              评论(0)              收藏
		文件存储 这是本人(菜鸟)学习android数据存储时接触的有关文件存储的知识以及本人自己写的简单地demo,为初学者学习和使用文件存储提供一些帮助.. 如果有需要查看SharedPreference ... 
- numpy的文件存储.npy .npz 文件详解
		Numpy能够读写磁盘上的文本数据或二进制数据. 将数组以二进制格式保存到磁盘 np.load和np.save是读写磁盘数组数据的两个主要函数,默认情况下,数组是以未压缩的原始二进制格式保存在扩展名为 ... 
- .Net下二进制形式的文件存储与读取
		.Net下图片的常见存储与读取凡是有以下几种:存储图片:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[].1.参数是图片路径:返回 ... 
随机推荐
- nyi63——树
			#include<bits/stdc++.h> using namespace std; int cnt; struct node { int data; int flag; node * ... 
- yii2的定时任务
			php yii minsheng-cancel-account/cancel-applied-account 
- 清华大学 pip 源
			pypi 镜像使用帮助 pypi 镜像每 5 分钟同步一次. 临时使用 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-pac ... 
- 013PHP基础知识——流程控制(一)
			<?php /** * 13 流程控制(一) * if语句: if(表达式){ 表达式 }elseif(表达式){ 代码段 } * if语句中,一个条件成立,其他分支不执行. * if中的表达式 ... 
- 009-对象—— 构造方法__construct析构方法__destruct使用方法 PHP重写与重载
			<?php /**构造方法__construct析构方法__destruct使用方法 PHP重写与重载 */ //构造方法:当实例化对象时,自动运行的方法 /*class channel{ fu ... 
- [JS]JavaScript判断操作系统版本
			function detectOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.platform == " ... 
- rsync的服务启动脚本
			#!/bin/bash #author:Mr.chen # chkconfig: # description:This is Rsync service management shell script ... 
- zoj1654
			题解: 对于每一联通的x,y 检点 然后交叉的连边 然后二分图 代码: #include<cstdio> #include<cstring> #include<cmath ... 
- PHP exec()函数的介绍和使用DEMO
			exec()函数用来执行一个外部程序,我们再用这函数基本是在linux. 开启exec()函数: exec()函数是被禁用的,要使用这个函数必须先开启.首先是 要关掉 安全模式 safe_mode = ... 
- EFM32  ARM+ KEIl program
			1Hardware connection When using the EFM32 starter kit to make a JLINK burn, you must connect the con ... 
