1

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)
sjp
  • 382
  • 4
  • 15
  • Apparently I can't answer my own question yet, but the problem is solved, see comment on User's answer – sjp Nov 20 '13 at 12:04

3 Answers3

1

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
User
  • 14,131
  • 2
  • 40
  • 59
  • well, `v = genfromtxt(...` certainly returns something, without errors, problem is it's a seemingly useless `array([('', ''), ('', ''), ('', ''), ..., ` using only one column, I get a neat array of floats, and I want to get two of those. I've done it before with x, y = loadtxt(... but the footer of this tsv means I cannot go that route – sjp Nov 20 '13 at 11:48
  • Apparently, the problem was in the converter. Modified it to `return re.sub('\,', '.', str(s)[2:-1])` and now it all works stellar. Seems strange that it was ok on single columns before though. Oh well.. – sjp Nov 20 '13 at 12:02
0

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))
Community
  • 1
  • 1
Steinar Lima
  • 7,644
  • 2
  • 39
  • 40
0

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

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