python读取txt中数据转为列表

import numpy as np

#直接读取
a=np.loadtxt("D:/Desktop/a.txt")
print(type(a),a,np.shape(a))

#自己编一个读取
data_list=[]
tmep=[]
with open("D://Desktop//a.txt","r") as f:
    file = f.readlines()
    #print(file)
    for line in file:
        line=line.strip("\n")#去除末尾的换行符
        dd=line.split()#拆分为两个元素,再对每个元素实行类型转换
        temp=list(map(float,dd))
        data_list.append(temp)
    data=np.array(data_list)
    print(data,np.shape(data))

result:

[[-3.5024832   1.7643725 ]
 [ 3.2294108   3.3832441 ]
 [ 1.2080167  -0.49958021]
 ...
 [27.11986     8.6698061 ]
 [25.962327    8.1703497 ]
 [24.913635    8.5649205 ]] (1400, 2)
[[-3.5024832   1.7643725 ]
 [ 3.2294108   3.3832441 ]
 [ 1.2080167  -0.49958021]
 ...
 [27.11986     8.6698061 ]
 [25.962327    8.1703497 ]
 [24.913635    8.5649205 ]] (1400, 2)
 

你可能感兴趣的:(python,python)