将文件读入数组只会产生前3列

时间:2014-11-20 00:57:31

标签: python arrays numpy

我尝试将我的csv文件读入一个numpy数组结构,虽然它似乎工作, 我只能找回每行的前3列。

下面是我的csv文件示例 - 我只粘贴了前5列,因为它会很大。 行尾字符为"\r\n"

1,2,3,4,5,..
061110-15-14,061110-15-18,061110-15-22,061210-15-02,061210-15-06,...
0.085539622,-0.518607931,0.072114121,1.763727267,-0.679713944,...
1.058257011,-0.473227862,-0.527200897,0.309381148,-0.473227862,...

这是我的代码:

import numpy
from numpy import dtype, loadtxt, float64, NaN, isfinite, all

# Open the file.
log_file = open('metab_averaged_zscored.csv')

# 1.Create a dtype from the names in the file header.
header = log_file.readline()
samples = log_file.readline()
log_names = samples.split()

fields = zip(log_names, ['f8']*len(log_names))
fields_dtype = dtype(fields)
logs = numpy.loadtxt(log_file, dtype=fields_dtype, delimiter = ",")

我得到的是以下内容:

 logs = array([(0.085539622, -0.518607931, 0.072114121),
   (1.058257011, -0.473227862, -0.527200897),
   (1.466116577, 0.899374241, -0.466269943),
   (0.402747391, -0.334736177, -0.838561584),
   (0.130944318, 1.047554546, -0.652548242),
   (0.796330151, 1.154931255, -0.329980359),
   (1.236012671, 0.32536557, -0.453508307),
   (0.75888538, 0.120736819, -1.13594891),
   (1.253438842, -0.307437261, -0.801444111),
   (1.486744816, -0.632472495, -0.793814719),
   (1.14192242, 0.167864804, -1.485382644),
   (-0.439353401, -0.190430786, -0.306749765),
   (0.624746908, 0.859866713, 0.046744056),
   (0.867743161, 0.605924104, -0.730731083)], 
  dtype=[('061110-15-14,061110-15-18,061110-15-22,061210-15-02,061210-15-06,061210-15-10',   '<f8'),    .....

但我的输入文件长49列,其余的去哪了?

2 个答案:

答案 0 :(得分:1)

我认为您可以使用genfromtxt代替loadtxt来简化操作。试试这个单行:

data = numpy.genfromtxt('metab_averaged_zscored.csv', delimiter=',', skip_header=1, names=True)

例如,

In [73]: data = numpy.genfromtxt('metab_averaged_zscored.csv.csv', delimiter=',', skip_header=1, names=True)

In [74]: data
Out[74]: 
array([(0.085539622, -0.518607931, 0.072114121, 1.763727267, -0.679713944),
       (1.058257011, -0.473227862, -0.527200897, 0.309381148, -0.473227862)], 
      dtype=[('0611101514', '<f8'), ('0611101518', '<f8'), ('0611101522', '<f8'), ('0612101502', '<f8'), ('0612101506', '<f8')])

(请注意genfromtxt从字段名称中删除了破折号。)

答案 1 :(得分:0)

我认为问题在于这一行:

log_names = samples.split()

这将仅按空格分割,但看起来您的列也使用逗号指定。试试这个:

log_names = samples.split(',')

这将仅以逗号分隔。