Python将数据文件读入列表或数组

use*_*056 3 python file-io

出于某种原因,我决定将我的数据列表保存为字符串.如果我使用

f = open('tarfile.txt')
for line in f.readlines():
    print line
f.close()
Run Code Online (Sandbox Code Playgroud)

我的输出看起来像:

[  53.7775   13.4375   26.525    48.63    125.      185.      653.    ]    
[  53.7775    13.33625   26.73375   48.68375  125.       185.       653.     ]    
[  53.7775    13.325     27.11375   48.8875   126.       187.       653.     ]    
[  53.7775    13.43625   27.3175    48.92875  126.       187.       653.     ]    
[  53.7775    14.4825    33.07375   51.7325   141.       202.       595.     ]
Run Code Online (Sandbox Code Playgroud)

我想将这些数据读入2D数组.我搜索并尝试了各种方法,如泡菜,eval,json等,但没有任何效果

Alo*_*hal 5

如果您正在使用numpy(或者不介意使用它),您可以这样做numpy.loadtxt('tarfile.txt', usecols=range(1,8)).如果您想要将数据放在二维numpy数组中,那就特别好了.