I have a small amount of data in a file which I want to use to draw plots. I can't see any problem with the following code, but running it results in the error message below.

`a,b,c = np.loadtxt('myfile.csv', delimiter=',', usecols=(0,12,18), skiprows=1, dtype=('S5, i4, S8'), unpack=True)`

ValueError: too many values to unpack

I know that this error message usually means that there is an imbalance between the left and right sides of the statement, but in the code above a,b,c then 0,12,18 and S5, i4, S8 all refer to 3 sets of data.

Am really scratching my head with this one - any advice greatfully received.

I do - good point though. I've changed my code to replace '18' with '17', since the first column is '0', but still get the same error message.

Thanks for responding

What does the array look like that you get if you use ...
arr = np.loadtxt('myfile.csv', delimiter=',', usecols=(0,12,18), skiprows=1, dtype=('S5, i4, S8'), unpack=False)

Edited 4 Years Ago by vegaseat: 1

Like this:

array([('"F50.', 229, '74734'), ('"F50.', 3, '2411'), ('"F50.', 2, '2207'),
       ('"F50.', 0, '139'), ('"F50.', 30, '135'), ('"F50.', 9, '525'),
       ('"F50.', 14, '2681'), ('"F50.', 30, '5'), ('"F50.', 247, '82610'),
       ('"F50.', 8, '1767'), ('"F50.', 4, '3736'), ('"F50.', 0, '277'),
       ('"F50.', 3, '3'), ('"F50.', 10, '143'), ('"F50.', 20, '1469'),
       ('"F50.', 28, '8'), ('"F50.', 278, '77296'), ('"F50.', 12, '3222'),
       ('"F50.', 4, '1750'), ('"F50.', 0, '115'), ('"F50.', 0, '69'),
       ('"F50.', 14, '154'), ('"F50.', 20, '1413'), ('"F50.', 26, '17'),
       ('"F50.', 288, '70919'), ('"F50.', 9, '2827'), ('"F50.', 3, '2682'),
       ('"F50.', 1, '40'), ('"F50.', 1, '143'), ('"F50.', 14, '203'),
       ('"F50.', 15, '1127'), ('"F50.', 29, '6'), ('"F50.', 307, '86151'),
       ('"F50.', 9, '2073'), ('"F50.', 3, '3332'), ('"F50.', 0, '278'),
       ('"F50.', 1, '169'), ('"F50.', 10, '158'), ('"F50.', 23, '2586'),
       ('"F50.', 28, '7'), ('"F50.', 240, '77824'), ('"F50.', 9, '2479'),
       ('"F50.', 3, '2236'), ('"F50.', 0, '716'), ('"F50.', 0, '500'),
       ('"F50.', 13, '696'), ('"F50.', 40, '2253'), ('"F50.', 27, '7')], 
      dtype=[('f0', '|S5'), ('f1', '<i4'), ('f2', '|S8')])

Which seems to indicate that the parameters are correct. (Apologies if this isn't formatted properly - I'm not sure whether results should be inserted as code or not).

your have 16*3=48 values and you are trying to put them in three variables, maybe you mean to assign the array values transposed, but left out trasposing?

This might clear things up a little:

# test module numpy loadtxt()
# Python27

import numpy as np
from StringIO import StringIO

# csv type data for test
data_str = """\
10,11,12,13,14,15,16,17,18,19
20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39
40,41,42,43,44,45,46,47,48,49
50,51,52,53,54,55,56,57,58,59
60,61,62,63,64,65,66,67,68,69
70,71,72,73,74,75,76,77,78,79
"""

# create a file object
data_file = StringIO(data_str)

arr = np.loadtxt(data_file, delimiter=',', usecols=(0,2,8), 
                 skiprows=1, dtype=('S5, i4, S8'), unpack=True)

print(arr)

'''
[('20', 22, '28') ('30', 32, '38') ('40', 42, '48') ('50', 52, '58')
 ('60', 62, '68') ('70', 72, '78')]
'''

# now unpack one tuple at a time
for a,b,c in arr:
    #print(a,b,c)
    print a, b, c

'''
20 22 28
30 32 38
40 42 48
50 52 58
60 62 68
70 72 78
'''

# check types
print type(a), type(b), type(c)

'''
<type 'numpy.string_'> <type 'numpy.int32'> <type 'numpy.string_'>
'''
Comments
useful

Thank you both. Really helpful. I tried HiHe's code and it works. Now I just need to re-read the manual to fully understand what I should have done! Thanks again.

This article has been dead for over six months. Start a new discussion instead.