http://db.lcs.mit.edu/labdata/labdata.html我想要读取并做进一步计算的数据
这是我要从txt文件读取的数据,并将其存储在numpy数组中。
filename = 'data5.txt'
with open(filename, 'r') as filehandle:
filecontent = filehandle.readlines()
print('filecontent[0]',filecontent[0])
data = np.empty([len(filecontent), 6])
print('Np array', data)
for i in range(len(filecontent)):
data_row = []
x = filecontent[i].split(' ')
data_row = [x[0], x[1], x[4], x[5], x[6], x[7]]
data[i] = data_row
print('data[0]', data[0])
我希望得到此输出。
filecontent[0]['2004-03-31', '03:38:15.757551', '2', '1', '122.153', '-3.91901', '11.04', '2.03397']
data[0][2004-03-31, 03:38:15.757551, 122.153, -3.91901, 11.04, 2.03397]
但输出已收到
['2004-03-31', '03:38:15.757551', '2', '1', '122.153', '-3.91901', '11.04', '2.03397\n']
Traceback (most recent call last):`
File "D:\workspace\TestPythonProject\src\practice1.py", line 252, in <module>
data[i] = data_row
ValueError: could not convert string to float: '2004-03-31'
我尝试了此线程,但它给了我一个错误convert date column in a text file to float