1
from scipy.io.wavfile import read

filepath = glob.glob('*.wav') #list

rate,data = [read(fp) for fp in filepath]

and I get :

ValueError: too many values to unpack

but i can load only once a time.

like this:

rate,data = read('001.wav')
print rate
print data      

and I get:

44100
[0 0 0 ..., 0 0 0]

How could I load the rate and data to two arrays, and if I do :

datas = [read(fp) for fp in filepath]

I will get :

[(44100, array([0, 0, 0, ..., 0, 0, 0], dtype=int16)), (44100, array([0, 0, 0, ..., 0, 0, 1], dtype=int16)), ..., (44100, array([0, 0, 0, ..., 0, 0, 0], dtype=int16))]

or there is any way I can split the datas to rate and data after loading.

Thanks.

user2858910
  • 109
  • 1
  • 1
  • 7
  • 2
    `[read(fp) for fp in filepath]` give more then two values. – Grijesh Chauhan Oct 08 '13 at 14:41
  • what do you mean for more then two values? – user2858910 Oct 08 '13 at 14:50
  • 1
    Try this code on your interpreter: `>>>a, b = [1, 2, 3]` it give you same exception. because rhs have two variables `a` and `b` where lhs has three values to unpack `1`, `2`, `3`. – Grijesh Chauhan Oct 08 '13 at 14:52
  • I think you need a better data structure! – Vivek Oct 08 '13 at 14:54
  • How to fix the problem of my code, I can do rate,data = read('001.wav') and datas = [read(fp) for fp in filepath], but I can't do rate,data = [read(fp) for fp in filepath]. How could I load them use [read(fp) for fp in filepath] and puts data into two array? Thanks. – user2858910 Oct 08 '13 at 14:56

2 Answers2

1

You can use zip(*x) to format them into one list of rates and one list of the data lists.

raw_data = [read(fp) for fp in filepath]
rates, data = zip(*raw_data)

For example,

raw_data = zip(range(10), range(20, 30))
# [(0, 20), (1, 21), (2, 22), (3, 23), (4, 24), (5, 25), (6, 26), (7, 27), (8, 28), (9, 29)]

zip(*raw_data)
# [(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (20, 21, 22, 23, 24, 25, 26, 27, 28, 29)]
wflynny
  • 18,065
  • 5
  • 46
  • 67
1

You can split it after loading using the inverse zip function (as explained in this answer):

In [13]: rate, data = zip(*datas)

In [14]: rate
Out[14]: (44100, 44100)

In [15]: data
Out[15]: (array([0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 1]))

I would say that this is an acceptable way to do it in this scenario, although I think using a more classical for loop, where you append to lists, without using list comprehension, would be cleaner:

rates = []
datas = []
for fp in filepath:
    rate, data = read(fp)
    rates.append(rate)
    datas.append(data)

The reason you got a ValueError in the first case is because you are unpacking a list of all tuples for each of your files (obviously more than 2 given the error message) into only two variables - rate and data.

Community
  • 1
  • 1
metakermit
  • 21,267
  • 15
  • 86
  • 95
  • Thank you.If I want to make (array([0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 1, 0])) to (array([0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 1, 0])),what should I done? – user2858910 Oct 08 '13 at 15:20
  • You can use `np.vstack(data)` as explained [here](http://stackoverflow.com/questions/10104245/python-numpy-combine-array), but you should try googling these things first, before asking here (I just searched for 'python combine arrays into matrix') :) – metakermit Oct 08 '13 at 15:25
  • I only have the data array that contains (array([0, 0, 0, ..., 0, 0, 0], dtype=int16), array([0, 0, 0, ..., 0, 0, 1], ..., dtype=int16), array([0, 0, 0, ..., 0, 0, 0], dtype=int16)),if use np.vstack(data), I didn't know how to split data to each array then I can use np.vstack(each array) to combine it. – user2858910 Oct 08 '13 at 15:47
  • OK, I don't quite follow you, but in any case if you have some general problems understanding the data structures, I would first point you to read on the [numpy data structures](http://wiki.scipy.org/Tentative_NumPy_Tutorial) and [list and tuples in Python](http://docs.python.org/3/tutorial/datastructures.html). If you still have some specific questions after reading about them, open a new SO question. It is hard to read anything too detailed in these comments. – metakermit Oct 08 '13 at 15:59
  • I was trying what you suggest, that numpy.vstack([datas]) will creat array like this: [[array([0, 0, 0, ..., 0, 0, 0], dtype=int16) array([0, 0, 0, ..., 0, 0, 1], dtype=int16) array([0, 0, 0, ..., 0, 0, 0], dtype=int16) array([0, 0, 0, ..., 0, 0, 0], dtype=int16) array([0, 0, 0, ..., 0, 0, 0], dtype=int16) array([0, 0, 0, ..., 0, 0, 0], dtype=int16) array([0, 0, 0, ..., 0, 0, 0], dtype=int16) array([0, 0, 0, ..., 0, 0, 0], dtype=int16) array([0, 0, 0, ..., 0, 0, 0], dtype=int16) array([0, 0, 0, ..., 0, 0, 0], dtype=int16)]]. – user2858910 Oct 08 '13 at 16:46
  • In fact, I prefer to get like this : (array([[ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], ..., [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0., 0., ..., 0., 0., 0.]], dtype=float32)). My information is the datas is a list, so I need to add [], so numpy.vstack([datas]) would work without error,but I still try to find the way to make the array that I wish to use.Thanks. – user2858910 Oct 08 '13 at 16:50
  • This is a new SO question : http://stackoverflow.com/questions/19254618/python-numpy-list-to-array-and-vstack Thanks for all of the people who help me ever. – user2858910 Oct 08 '13 at 17:44
  • Sure, no problem. I see you got a good answer there already :) – metakermit Oct 09 '13 at 07:32