python:将txt文档中是数值型数据读入到array数组中

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/littlle_yan/article/details/79302488
1.介绍我的txt文件内容是这样的,准确来说是在excle表格中复制粘贴到txt文档中的(这里的一列介绍excle中的一列,一行就是excle中的一行),如图所示:


2.执行如下代码,特别说明一下,k值的含义是txt中的列数,本实验是6列,所以k的赋值为6:
#-*-coding:UTF-8 -*-
import numpy as np
def loadDatadet(infile,k):
    f=open(infile,'r')
    sourceInLine=f.readlines()
    dataset=[]
    for line in sourceInLine:
        temp1=line.strip('\n')
        temp2=temp1.split('\t')
        dataset.append(temp2)
    for i in range(0,len(dataset)):
        for j in range(k):
            dataset[i].append(float(dataset[i][j]))
        del(dataset[i][0:k])
    return dataset
infile='F:\pycharm\h5py_torch\hdf5_format\\test.txt'
k=6
infile=np.array(loadDatadet(infile,k))
print('dataset=',infile)
3.执行结果如图所示:




猜你喜欢

转载自blog.csdn.net/littlle_yan/article/details/79302488