目录
1 np.save()
进进保存一个数组,不是很推荐使用。
np.save(file, arr, allow_pickle=True, fix_imports=True)
2 np.savez()
如果你想将多个数组保存到一个文件中的话,可以使用numpy.savez函数。savez函数的第一个参数是自定义的文件名,其后的参数都是需要保存的数组变量,也可以使用关键字参数为数组起一个名字,非关键字参数传递的数组会自动起名为arr_0, arr_1, …。
ar1 = np.random.rand(2,3)
ar2 = np.arange(4)
np.savez(r'C:\python析\test.npz',ar1,ar2)
# 自定义命名方法
np.savez(r'C:\python析\test.npz',my_name1=ar1, my_name2=ar2)
3 np.load()
np.load()用于打开npz(包含多个数组)、npy(单个数组)文件。
3.1 参数
np.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding=‘ASCII’)
file : file-like object, string, or pathlib.Path. The file to read. File-like objects must support the seek() and read() methods. Pickled files require that the file-like object support the readline() method as well.
mmap_mode : {None, ‘r+’, ‘r’, ‘w+’, ‘c’}, optional. If not None, then memory-map the file, using the given mode :
mmap_mode | 具体操作 |
---|---|
‘r’ | Open existing file for reading only. |
‘r+’ | Open existing file for reading and writing. |
‘w+’ | Create or overwrite existing file for reading and writing. |
‘c’ | Copy-on-write: assignments affect data in memory, but changes are not saved to disk. The file on disk is read-only. |
encoding : str, optional. What encoding to use when reading Python 2 strings. Only useful when loading Python 2 generated pickled files on Python 3, which includes npy/npz files containing object arrays. Values other than(除了) ‘latin1’, ‘ASCII’, and ‘bytes’ are not allowed, as they can corrupt numerical data. Default: ‘ASCII’
不重要的参数我就没写啦
3.2 先查看npz文件有几个数组文件
with np.load(file_name) as f:
print(f.files)
#也可以
#f = np.load(file_name)
得到结果
['arr_1', 'arr_0']
说明只有两个文件,所以我可以他们的名字,具体地打开他们
3.3 打开文件
with np.load(file_name) as f:
array_0, array_1 = f['arr_1'], f['arr_0']
还有一种方法,就是用列表解析遍历所有
with np.load(file_name) as f:
array_0, array_1 = [f['arr_%d' % i] for i in range(len(f.files))]
得到结果:
print(array_0)
print(array_1)
>>>
[ 0 3 4 ... 48997 48998 48999]
[20626 4971 29315 ... 41341 18543 4568]
4 with open()
读数据
with open('filename.txt') as f:
str = f.read() #文件的读操作
4.1 写数据
with open('data.txt', 'w') as f:
f.write('hello world') #文件的写操作
f.flush() #可以使得立刻讲数据写入文件,也就是说,在f.close()前你就可以看到文件里写入数据
4.2 参数
前面讲的默认都是读取文本文件,并且是UTF-8编码的文本文件。要读取二进制文件,比如图片、视频等等,用’rb’模式打开文件即可
r: 以只读方式打开文件。文件的指针将会放在文件的开头。这是**默认模式**。
rb: 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。
r+: 打开一个文件用于读写。文件指针将会放在文件的开头。
rb+:以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。
w: 打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb: 以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
w+: 打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb+:以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a: 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
ab: 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+: 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+:以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。
4.3 file对象的属性
file.read([size]) 将文件数据作为字符串返回,可选参数size控制读取的字节数
file.readline() 返回文件中的某一行,以\n为一行结束的标志
file.readlines([size]) 返回文件中行内容的列表,size参数可选
file.write(str) 将字符串写入文件
file.writeline(strings) 将字符串序列写入文件
file.writelines(strings) 将字符串序列写入文件
file.close() 关闭文件
file.closed 表示文件已经被关闭,否则为False
file.mode Access文件打开时使用的访问模式
file.encoding 文件所使用的编码
file.name 文件名
file.newlines 未读取到行分隔符时为None,只有一种行分隔符时为一个字符串,当文件有多种类型的行结束符时,则为一个包含所有当前所遇到的行结束的列表
file.softspace 为0表示在输出一数据后,要加上一个空格符,1表示不加。这个属性一般程序员用不着,由程序内部使用
4.4 遇到汉字怎么办?
4.4.2 方法一
得益于 open() 里的 encoding参数,我们可以方便地读写中文数据:
with open(folder_name+"/"+"record.txt","a", encoding="utf-8") as f:
str0 = f"当delta={delta}时,F_train={F_train}, F_test={F_test}"
f.write(str0) # 以防万一可以在str0前面加上u,但是这里好像没法这样操作
f.write("\n")
with open(folder_name+"/"+"record.txt","r", encoding="utf-8") as f:
str0 = f.readline(3)
4.4.1 方法二
如果用 with open(‘data.txt’, ‘w’) as f: 这样保存的汉字再打开将会是乱码,所以要先把str类型的字符串转成二进制类型的字符串再保存,转换方式很简单:
bytes --> str s = s.decode()
str --> bytes s = s.encode()
来看一个例子,先保存后打开:
# 保存
with open(folder_name+"/"+"record.txt","ab") as f: # b 对应 binary
str0 = f"当delta={delta}时,F_train={F_train}, F_test={F_test}"
f.write(str0.encode())
f.write("\n".encode())
# 读取
with open(folder_name+"/"+"record.txt","rb") as f:
str1 = f.readlines(3)
参考:
https://blog.csdn.net/msspark/article/details/86745391