在數(shù)據(jù)分析中,經(jīng)常需要從文件中讀取數(shù)據(jù)或?qū)?shù)據(jù)寫入文件,常用的存儲(chǔ)文件的格式有文本文件、CSV格式文件、二進(jìn)制格式文件和多維數(shù)據(jù)文件等。
**numpy.savetxt(fname,array,fmt='%.18e',delimiter=None,newline='\n', header='', footer='', comments='# ', encoding=None)**
主要參數(shù):
fname:文件、字符串或產(chǎn)生器,可以是.gz 或.bz2 的壓縮文件
array:存入文件的數(shù)組(一維數(shù)組或者二維數(shù)組)
fmt:寫入文件的格式,如:%d,%.2f,%.18e,默認(rèn)值是%.18e 可選項(xiàng)
delimiter: 分隔符,通常情況是str可選
header:將在文件開頭寫入的字符串
footer:將在文件尾部寫入的字符串
comments: 將附加到header和footer字符串的字符串,以將其標(biāo)記為注釋。
默認(rèn)值:'#' encoding:用于編碼輸出文件的編碼。
import numpy as np arr = np.arange(12).reshape(3,4) #fmt缺省取%.18e(浮點(diǎn)數(shù)) #分割符默認(rèn)是空格,寫入文件保存在當(dāng)前目錄 np.savetxt('test-1.txt',arr) #fmt:%d 寫入文件的元素是十進(jìn)制整數(shù),分割符為逗號(hào)",",寫入文件保存在當(dāng)前目錄 np.savetxt('test-2.txt',arr,fmt='%d',delimiter=',') #在test-3.txt文件頭部和尾部增加注釋,頭部 #test-3,尾部 # 數(shù)據(jù)寫入注釋,寫入文件的元素是字符串 np.savetxt('test-3.txt',arr,fmt='%s',delimiter=',',header=\ 'test-3',footer='測(cè)試數(shù)據(jù)',encoding='utf-8') #在test-4.txt文件頭部加##test-4注釋 np.savetxt('test-4.txt',arr,fmt='%f',delimiter=',',header= 'test-4',comments='###') #將arr數(shù)組保存為csv文件 np.savetxt('test-1.csv',arr,fmt='%d',header='test-1')
numpy.loadtxt(fname,dtype=type'float'>,comments='#',delimiter=None, converters=None,skiprows=0,usecols=None,unpack=False,ndmin=0,encoding=‘bytes')
#參數(shù)說明:
fname:被讀取的文件名(文件的相對(duì)地址或者絕對(duì)地址)
dtype:指定讀取后數(shù)據(jù)的數(shù)據(jù)類型
comments: 跳過文件中指定參數(shù)開頭的行(即不讀?。?br />
delimiter:指定讀取文件中數(shù)據(jù)的分割符
converters: 對(duì)讀取的數(shù)據(jù)進(jìn)行預(yù)處理
skiprows:選擇跳過的行數(shù)
usecols:指定需要讀取的列
unpack:選擇是否將數(shù)據(jù)進(jìn)行向量輸出
encoding:對(duì)讀取的文件進(jìn)行預(yù)編碼
a = np.loadtxt('test-1.txt') #讀入當(dāng)前目錄下的文件 test-1.txt print(a) [[ 0. 1. 2. 3.] [ 4. 5. 6. 7.] [ 8. 9. 10. 11.]]
# skiprows:指跳過前1行, 如果設(shè)置skiprows=2, 就會(huì)跳過前兩行,數(shù)據(jù)類型設(shè)置為整型. a = np.loadtxt('test-1.txt', skiprows=1, dtype=int) print(a) [[ 4 5 6 7] [ 8 9 10 11]]
# comment, 如果行的開頭為#就會(huì)跳過該行 a = np.loadtxt('test-4.txt', skiprows=2, comments='#',delimiter=',') b = np.loadtxt('test-4.txt',comments='#',delimiter=',') print(a,b,sep='\n') [[ 4. 5. 6. 7.] [ 8. 9. 10. 11.]] [[ 0. 1. 2. 3.] [ 4. 5. 6. 7.] [ 8. 9. 10. 11.]]
# usecols:指定讀取的列,若讀取0,2兩列 aa = np.loadtxt('test-3.txt',dtype=int, skiprows=1,delimiter=',',usecols=(0, 2)) #unpack是指會(huì)把每一列當(dāng)成一個(gè)向量輸出, 而不是合并在一起。 (a, b) = np.loadtxt('test-2.txt', dtype=int, skiprows=1, comments='#', delimiter=',', usecols=(0, 2), unpack=True) print(aa,a, b,sep='\n') [[ 0 2] [ 4 6] [ 8 10]] [4 8] [ 6 10] #讀取csv文件 aa = np.loadtxt('test-1.csv',skiprows=1) print(aa) [[ 0. 1. 2. 3.] [ 4. 5. 6. 7.] [ 8. 9. 10. 11.]]
save函數(shù)將數(shù)組以未壓縮的原始二進(jìn)制格式保存在擴(kuò)展名為.npy的文件中。會(huì)自動(dòng)處理元素類型和形狀等信息。
savez函數(shù)將多個(gè)數(shù)組壓縮到一個(gè)擴(kuò)展名為npz的文件,其中每個(gè)文件都是一個(gè)save()保存的npy文件,文件名和數(shù)組名相同
save()或savez()函數(shù)的格式:
numpy.save(file,array) numpy.savez(file,array)
load()函數(shù)的格式: numpy.load(file)
import numpy as np a = np.arange(12).reshape(3,4) print('原數(shù)組a:\n',a) np.save('arr1.npy', a) #將數(shù)據(jù)存儲(chǔ)為npy,保存時(shí)可以省略擴(kuò)展名,默認(rèn).npy c = np.load('arr1.npy') #讀取arr1.npy的數(shù)據(jù),讀取數(shù)據(jù)時(shí)不能省略 .npy print('讀取后的數(shù)據(jù):\n',c) ar = np.arange(6).reshape(3,2) print('保存前的數(shù)組:',a,ar,sep='\n') np.savez('arr2.npz',a,ar) #多數(shù)組存儲(chǔ),默認(rèn)文件名.npz b = np.load('arr2.npz') print('讀取后的數(shù)據(jù):') print(b['arr_0'],b['arr_1'],sep='\n')
原數(shù)組a:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
讀取后的數(shù)據(jù):
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
保存前的數(shù)組:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[0 1]
[2 3]
[4 5]]
讀取后的數(shù)據(jù):
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[0 1]
[2 3]
[4 5]]
for i in b.items(): print(i) ('a', array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])) ('ar', array([[0, 1], [2, 3], [4, 5]]))
以上就是python使用NumPy文件的讀寫操作的詳細(xì)內(nèi)容,更多關(guān)于python使用NumPy讀寫文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:股票 錦州 天水 白城 西安 日照 隨州 安慶
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python使用NumPy文件的讀寫操作》,本文關(guān)鍵詞 python,使用,NumPy,文件,的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。