假设文本文件中保存的是形如矩阵的数据,但由于文本文件的格式所限,打开文件后按行读取到的类型是字符串,不方便后续按数值处理,所以需要进行类型转换。参考网查并在Python2.7.5中调试后,总结为以下两种方式:
(1)已知矩阵列数但行数未知(读取文件文件时获取行数)时:
from numpy import *
from FileDialog import *
import tkFileDialog
def txt2RealMat(): #define a function to get the real array
try:
filename=tkFileDialog.askopenfilename(initialdir='D:/') # the default path of .txt file
except ValueError:
pass
with open(filename,'r') as fr:
initDataRow=len(fr.readlines()) # get the number of rows
initDataCol=5 # suppose the default number of columns is 5
dataMat=zeros((initDataRow,initDataCol),dtype=float)
tempRow=0 # count the number of rows that have been read
with open(filename,'r') as fr:
for line in fr.readlines():
temp=line.strip('\n').split('\t')
dataMat[tempRow:]=temp
tempRow=tempRow+1
return dataMat
if __name__=="__main__":
dataMat=txt2RealMat()
print dataMat
(2)矩阵行数和列数均未知(都在读取文本文件时获取)时:
from numpy import *
from FileDialog import *
import tkFileDialog
def txt2RealMat():
try:
filename=tkFileDialog.askopenfilename(initialdir='D:/') # an open file dialog with default path of the .txt file
except ValueError:
pass
with open(filename,'r') as fr:
initDataRow=len(fr.readlines()) # get the number of rows
data=[]
with open(filename,'r') as fr:
for line in fr.readlines():
temp=line.strip('\n').split('\t') # read one line
data.append(temp)
initDataCol=len(temp) # get the number of columns
for i in range(initDataRow):
for j in range(initDataCol):
data[i][j]=float(data[i][j]) # type transformation
return mat(data)
# test the function
if __name__=="__main__":
dataMat=txt2RealMat()
print dataMat
该博客介绍了如何在Python2.7.5中将包含矩阵数据的文本文件转换为浮点型矩阵。提供了两种方法,一种是已知列数但行数未知的情况,另一种是行数和列数均未知的情况。方法包括使用numpy库,通过文件对话框选择文件,读取行数,分割数据并进行类型转换。
27

被折叠的 条评论
为什么被折叠?



