[英]Reading wav files in a folder with Python
Here is my code : 这是我的代码:
for filename in glob.glob('*.wav') :
for i in range(10) :
sr[i],x[i]=scipy.io.wavfile.read(filename)
I want to read .wav files in a folder and save their values on x and also their sampling rates on sr. 我想读取文件夹中的.wav文件,并将其值保存在x上,并将其采样率保存在sr上。 But my code gives error: "list assignment index out of range".
但是我的代码给出了错误:“列表分配索引超出范围”。 How can I fix it?
我该如何解决? I am using python2.7 on ubuntu.
我在ubuntu上使用python2.7。
I could be wrong but I'm guessing it'll be the line: 我可能是错的,但我猜这将是台词:
sr[i],x[i] = scipy.io.wavfile.read(filename)
I don't think you'll be able to index the results like that as I dont think they're lists. 我认为您无法像这样列出结果,因为我认为它们不是列表。
try this instead: 试试这个代替:
sr, x = scipy.io.wavfile.read(filename)
If you need a list of the sr
and x
values, try defining the lists before reading the files. 如果需要
sr
和x
值的列表,请在读取文件之前尝试定义列表。 Append the results individually to each of your lists. 将结果分别追加到每个列表中。 There is probably a clever way of writing this, but for clarity I'll just do it like this:
可能有一种聪明的编写方式,但是为了清楚起见,我将这样做:
sr = []
x = []
for filename in glob.glob('*.wav'):
for i in range(10):
sr_value, x_value = scipy.io.wavfile.read(filename)
sr.append(sr_value)
x.append(x_value)
I'd also maybe recommend renaming your sr
and x
(particularly x
) variables to something more meaningful. 我也可能建议将您的
sr
和x
(尤其是x
)变量重命名为更有意义的名称。 It might make your code a bit easier to read and it could save you a few naming conflicts later down the line. 这可能会使您的代码更易于阅读,并且可以在以后省去一些命名冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.