将csv文件读入以下格式

时间:2016-04-13 13:29:07

标签: csv numpy

  

[array([0.33333333,0.75,0.5,1。,1   ,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0,0.5,0.2,0.25,0.6,           0.8,0.5]),数组([1.,0.25,1.1,1.0,0.0,0.0,1。,1,           0,0,0,0,0,0,0,0,0,0,0,0,           阵列([1,0.25,0.5,0.5,0.0,0.0,0.0,0.1,0.0,0.0,0.25,0.25]) 。,           0,0,0,0,0,0,0,0,0,0,0,0,           数组,(0.66666667,0.25,0.5,1。,0。   ,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0。,1.,0.4,0.25,0.4,           0.6,0.75]),数组([0.66666667,0.5,1.1,1。,0,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0。,0.5,1。,0.5,0.6,           0.2,0.75]),数组([0.33333333,0.5,1.1,1。,0,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0,0.5,0.2,0.75,0.4,           0.8,0.75]),数组([0.66666667,0.5,0.5,1。,0,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0。,0.5,1。,0.5,0.8,           1.,0.25]),数组([0.33333333,0.25,1.1,1。,0,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0。,1.,1.,0.25,0.2,           0.6,0.5]),数组([0.66666667,0.5,0.5,1。,0,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0。,1.,0.8,0.75,1。,           0.2,0.75]),数组([1.,0.5,1.1,1.0,0.0,0.0,1.0,0。,0,           0,0,0,0,0,0,0,0,0,0,0,0,1,1。           0.8,1.,0.4,0.2,1。]),数组([0.33333333,0.25,1.1,1。,0,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0。,1.,0.6,0.25,0.2,           0.4,0.5]),数组([0.33333333,0.75,0.5,0.5,0。,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0。,0.5,1.,0.25,0.8,           0.4,0.75]),数组([0.66666667,0.75,0.5,0.5,0。,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0,0.5,0.6,0.25,0.4,           0.2,0.75]),数组([0.66666667,0.1,1。,1.0,0,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0,0.5,0.6,0.25,0.8,           1.,1。]),数组([0.33333333,1。,1.,1.,0,           0.,0.,0.,1.,0,           0.,0.,0.,0.,0,           0.,0.,0.,0.,0,           0。,1.,0.4,0.25,0.6,           0.4,1。]),数组([1.,0.75,0.5,1.0,0.0,0.0,0.1,1。           0,0,0,0,0,0,0,0,0,0,0,0,           0.,0.,0.,0.5,0.6,0.25,1。,1.,0.5])]

我有文件阅读此内容但我想以[[0.33333333,0.75,0.5,1.,0.,0., 0., 0.,1.,0.,0.,0.,0.,0. ,0.,0.,0.,0.,0.,0.,0.,0.5,0.2,0.25, 0.6,0.8 ,0.5 ],[....],[....],[....]]格式

下面是我的代码,请查看它..感谢您的帮助

import csv
import numpy as nm
my_data = nm.genfromtxt('ta.csv',dtype=float,delimiter=',',usecols=range(4,31))

data=list(my_data)
#input_data=data
print(data)

请给我建议....再次感谢

3 个答案:

答案 0 :(得分:0)

我认为问题是list()命令。

如果我采用二维数组:

In [322]: data
Out[322]: 
array([[120258560,         3],
       [167772160,       339],
       [118948100,         1],
       [ 50331904,         1]])

并将其包装在list()中,我得到一个数组列表 - 每行一个数组。

In [323]: list(data)
Out[323]: 
[array([120258560,         3]),
 array([167772160,       339]),
 array([118948100,         1]),
 array([50331904,        1])]

您为什么使用list

有时genfromtxt给出一个结构化数组,1d带有命名列。但看起来它像所有花车一样读取你的文件。所以my_data应该是你想要的形式。

==============================

数组(和列表)可以以各种方式显示 - 也可以使用自定义方式。让我用一个简单的数组来说明 - 在交互式ipython shell中工作

In [6]: x=np.array([[1,2],[3,4]])

In [7]: x
Out[7]: 
array([[1, 2],
       [3, 4]])

您的原始方法 - 将数组转换为2个数组的列表:

In [8]: list(x)
Out[8]: [array([1, 2]), array([3, 4])]

要完整转换为嵌套列表,请使用tolist方法:

In [9]: x.tolist()
Out[9]: [[1, 2], [3, 4]]

In [10]: print x
[[1 2]
 [3 4]]

注意xprint x之间的区别。后者使用数组的str格式化方法。前者是repr方法。这些方法是为每个类定义的,但通常repr给出了更详细的视图。

In [11]: str(x)
Out[11]: '[[1 2]\n [3 4]]'

In [12]: repr(x)
Out[12]: 'array([[1, 2],\n       [3, 4]])'

数组的str格式省略了逗号,而列表的格式包含逗号。

对于结构化数组(例如使用names=True创建),()替换[]

In [15]: y=np.array([('a',1),('b',2)],dtype='str,int')

In [16]: y
Out[16]: 
array([('', 1), ('', 2)], 
      dtype=[('f0', 'S'), ('f1', '<i4')])

注意,这是一个带有复合dtype的1d数组。您不索引列,而是使用字符串索引字段。

打印(str)省略了dtype,看起来像一个元组列表。但请注意缺少的,

In [17]: print y
[('', 1) ('', 2)]

这两个list看起来一样:

In [18]: list(y)
Out[18]: [('', 1), ('', 2)]

In [19]: y.tolist()
Out[19]: [('', 1), ('', 2)]

但有一个微妙的区别

In [23]: type(list(y)[0])
Out[23]: numpy.void

In [24]: type(y.tolist()[0])
Out[24]: tuple

一个numpy个对象列表(结构化&#39;记录&#39;)另一个是元组列表。

请注意,当我定义y时,我使用元组列表。这是一个重要的细节。

答案 1 :(得分:0)

谢谢...... 但我不会将“,”赋予价值观 通过印刷它给[[1 2 3 5 9 8 10] [4 5 8 9 100 555] .... [] [] []] 但我希望在[[1,2,3,4,5],[3,4,5,6,3],[],[],[]这种格式中 我必须做什么才能获得这种格式? 有没有选项而不是列表?

答案 2 :(得分:0)

当我添加names = True

时 然后它给了我

[(1.0,0.25,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0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