在genfromtxt中解压缩的值太多了

时间:2013-11-20 11:14:53

标签: python numpy genfromtxt

我有一个包含振动数据的tsv文件(出于某些愚蠢的原因用逗号代替点,因此转换器)。我想从其中两个通道生成numpy数组,但是得到一个“ValueError:解压缩的值太多(预期2)”,这是我无法弄清楚的。

在ipython中(使用pylab选项):

In [171] import re

In [172]: def qdsub(s):
   .....:     return re.sub('\,', '.', str(s)[2:-1])
   .....:

In [173]: x, y = genfromtxt('QD1_short.tsv', delimiter='\t', usecols=(0, 1), 
   .....: unpack=True, skip_header=13, converters={0:qdsub, 1:qdsub}, 
   .....: skip_footer=2, dtype=float)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-177-e17389233ac3> in <module>()
        1 x, y = genfromtxt('QD1_short.tsv', delimiter='\t', usecols=(0, 1), 
        2 unpack=True, skip_header=13, converters={0:qdsub, 1:qdsub},
  ----> 3 skip_footer=2, dtype=float)

ValueError: too many values to unpack (expected 2)

3 个答案:

答案 0 :(得分:1)

也许这就是问题:

x, y = ge...

尝试

v = ge...

示例:

>>> a, b = [1,2,3,4]

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    a, b = [1,2,3,4]
ValueError: too many values to unpack

答案 1 :(得分:0)

numpy.genfromtxt的{​​{3}}说:

  

解包: bool,可选

If True, the returned array is transposed, so that arguments may be unpacked 
using x, y, z = loadtxt(...)

我最好的猜测是你应该尝试将genfromtxt包裹在loadtxt内:

from numpy import loadtxt, genfromtxt
(...)

x, y = loadtxt(genfromtxt('QD1_short.tsv', delimiter='\t', usecols=(0, 1), 
                          unpack=True, skip_header=13, converters={0:qdsub, 1:qdsub}, 
                          skip_footer=2, dtype=float))

答案 2 :(得分:0)

问题出在转换器上,显然应该返回一个浮点数

def qdsub(s):
    return float(re.sub('\,', '.', str(s)[2:-1]))