使用列表列表从TXT转换为数组错误

时间:2018-05-24 02:50:03

标签: python csv

我想将.txt文件转换为numpy数组,但是我收到以下错误:

import csv
import numpy as np
In [29]: with open('file.txt') as f:
    ...:     reader = csv.reader(f, delimiter='|')
    ...:     d = list(reader)
    ...:     d = np.array(d)
    ...: col1 = d[:,0]
    ...:
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-29-61025b336d31> in <module>()
      4     import numpy as np
      5     d = np.array(d)
----> 6 col1 = d[:,0]

IndexError: too many indices for array

d的格式如下:

In [30]: d
Out[30]:
array([list(['AF', 'AAUS', '9.0349', '38.766499', '2437.800049', 'Addis Ababa University, Ethiopia', '2006-01-10T00:00:00', ''])], dtype=object)

我想知道我做错了什么,因为我无法索引这个列表数组。您建议修复此错误的解决方法是什么? 感谢。

0 个答案:

没有答案