我是Python的新手,但是其他语言的经验丰富的兽医,但为什么data.dtype会抛出错误AttributeError: 'tuple' object has no attribute 'dtype'
,我怎样才能获得dtype值?
来自this博客看起来我应该能够data.dtype
代码
import pyaudio
import sys
import numpy
import scipy.io.wavfile
# Ensures that a file is passed in as an argument
if len(sys.argv) < 2:
print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
sys.exit(-1)
file = sys.argv[1];
# Read the file
data = scipy.io.wavfile.read(file)
rate = data[0]
print data
print "rate = ", rate
print data[1]
print "format = ", data.dtype
输出
(44100, array([[0, 0],
[0, 0],
[0, 0],
...,
[0, 0],
[0, 0],
[0, 0]], dtype=int16))
rate = 44100
[[0 0]
[0 0]
[0 0]
...,
[0 0]
[0 0]
[0 0]]
format =
Traceback (most recent call last):
File "index.py", line 31, in <module>
print "format = ", data.dtype
AttributeError: 'tuple' object has no attribute 'dtype'
答案 0 :(得分:1)
您获得此代码的博客有所不同:它将scipy.io.wavfile.read()
调用的结果解压缩为两个变量,而您的代码则没有。
sampFreq, snd = wavfile.read('440_sine.wav')
这意味着snd
有dtype
。在您的代码中,您有:
data = scipy.io.wavfile.read(file)
然后你必须访问你没有解压缩的元组的第二个元素:
vvv
print data[1]
print "format = ", data[1].dtype
^^^