Instructables开放源代码:Python索引器错误:列表索引超出范围
我在其他几个问题上看到了这个错误,但找不到答案。 {我对Python完全不熟悉,但我正在按照某个站点的说明进行操作,一旦我尝试运行脚本,就会不断出现以下错误:Instructables开放源代码:Python索引器错误:列表索引超出范围,python,Python,我在其他几个问题上看到了这个错误,但找不到答案。 {我对Python完全不熟悉,但我正在按照某个站点的说明进行操作,一旦我尝试运行脚本,就会不断出现以下错误: ##//txt to stl conversion - 3d printable record ##//by Amanda Ghassaei ##//Dec 2012 ##//http://www.instructables.com/id/3D-Printed-Record/ ## ##/* ## * This program is fr
##//txt to stl conversion - 3d printable record
##//by Amanda Ghassaei
##//Dec 2012
##//http://www.instructables.com/id/3D-Printed-Record/
##
##/*
## * This program is free software; you can redistribute it and/or modify
## * it under the terms of the GNU General Public License as published by
## * the Free Software Foundation; either version 3 of the License, or
## * (at your option) any later version.
##*/
import wave
import math
import struct
bitDepth = 8#target bitDepth
frate = 44100#target frame rate
fileName = "bill.wav"#file to be imported (change this)
#read file and get data
w = wave.open(fileName, 'r')
numframes = w.getnframes()
frame = w.readframes(numframes)#w.getnframes()
frameInt = map(ord, list(frame))#turn into array
#separate left and right channels and merge bytes
frameOneChannel = [0]*numframes#initialize list of one channel of wave
for i in range(numframes):
frameOneChannel[i] = frameInt[4*i+1]*2**8+frameInt[4*i]#separate channels and store one channel in new list
if frameOneChannel[i] > 2**15:
frameOneChannel[i] = (frameOneChannel[i]-2**16)
elif frameOneChannel[i] == 2**15:
frameOneChannel[i] = 0
else:
frameOneChannel[i] = frameOneChannel[i]
#convert to string
audioStr = ''
for i in range(numframes):
audioStr += str(frameOneChannel[i])
audioStr += ","#separate elements with comma
fileName = fileName[:-3]#remove .wav extension
text_file = open(fileName+"txt", "w")
text_file.write("%s"%audioStr)
text_file.close()
索引器:列表索引超出范围
以下是脚本:
##//txt to stl conversion - 3d printable record
##//by Amanda Ghassaei
##//Dec 2012
##//http://www.instructables.com/id/3D-Printed-Record/
##
##/*
## * This program is free software; you can redistribute it and/or modify
## * it under the terms of the GNU General Public License as published by
## * the Free Software Foundation; either version 3 of the License, or
## * (at your option) any later version.
##*/
import wave
import math
import struct
bitDepth = 8#target bitDepth
frate = 44100#target frame rate
fileName = "bill.wav"#file to be imported (change this)
#read file and get data
w = wave.open(fileName, 'r')
numframes = w.getnframes()
frame = w.readframes(numframes)#w.getnframes()
frameInt = map(ord, list(frame))#turn into array
#separate left and right channels and merge bytes
frameOneChannel = [0]*numframes#initialize list of one channel of wave
for i in range(numframes):
frameOneChannel[i] = frameInt[4*i+1]*2**8+frameInt[4*i]#separate channels and store one channel in new list
if frameOneChannel[i] > 2**15:
frameOneChannel[i] = (frameOneChannel[i]-2**16)
elif frameOneChannel[i] == 2**15:
frameOneChannel[i] = 0
else:
frameOneChannel[i] = frameOneChannel[i]
#convert to string
audioStr = ''
for i in range(numframes):
audioStr += str(frameOneChannel[i])
audioStr += ","#separate elements with comma
fileName = fileName[:-3]#remove .wav extension
text_file = open(fileName+"txt", "w")
text_file.write("%s"%audioStr)
text_file.close()
非常感谢,
LeartLeart-检查这些选项,它可能会有帮助:
*注意制表符空格哪一行引起错误?请注意,这是因为您正在访问的列表超出了它的长度。我建议,在询问更多问题之前,逐行调试代码并阅读@Pynchia在上面的评论上发布的内容。我真的不知道代码是如何工作的,所以我恐怕会重复在我把它发布到这里之前,不要把它搞砸了!错误出现在这一行:frameOneChannel[I]=frameInt[4*I+1]*2**8+frameInt[4*I]#单独的频道并将一个频道存储在新列表中感谢您的理解