python readline读文件,并且将其转化为数字形式时:ValueError: could not convert string to float:
读一个文本文件,总提示:ValueError: could not convert string to float;找了半天没找到哪儿有不可转化的字符,花了好长时间发现文本文件中最后有一个回车,无法转化为float,删除就可以了。
from numpy import* def load_dataset(filename): # 将数据存储到numpy的array中 fr = open(filename, 'r') data_mat = [] for line in fr.readlines(): line_arr = [] curline = line.strip().split('\t') for i in range(len(curline)): line_arr.append(float(curline[i])) data_mat.append(line_arr) data_arr = array(data_mat) if __name__ == '__main__': filename = '/home/jq/桌面/西瓜数据集3.0' load_dataset(filename)