Question

I have a tsv file containing vibration data (with commas in place of dots for some silly reason, hence the converter). I'd like to generate numpy arrays from two of these channels, but get a "ValueError: too many values to unpack (expected 2)" that I can't figure out.

in ipython (with pylab option):

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)
Était-ce utile?

La solution 3

The problem was in the converter, which apparently should return a float

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

Autres conseils

Maybe this is the problem:

x, y = ge...

Try

v = ge...

Example:

>>> 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

The documentation of numpy.genfromtxt says:

unpack : bool, optional

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

My best guess is that you should try to wrap the genfromtxt inside 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))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top