Instructables开放源代码: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

我在其他几个问题上看到了这个错误,但找不到答案。 {我对Python完全不熟悉,但我正在按照某个站点的说明进行操作,一旦我尝试运行脚本,就会不断出现以下错误:

##//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()
非常感谢,
Leart

Leart-检查这些选项,它可能会有帮助:

  • 你的输入文件格式正确吗?在我看来,你需要先生成该文件,然后才能在这个程序中使用它…在这里发布该文件
  • 检查您的比特率和帧率是否正确
  • 仅用于调试目的(如果代码正确,这可能不会产生正确的结果,但有利于测试)。您正在访问frameInt[4*i+1],索引i乘以4,然后再加1(最终超出frameInt索引)。 在访问frameInt中的数组元素之前,添加“如果”以检查大小: 如果len(frameInt)>=(4*i+1): 在第一次出现“for i in range(numframes):”之后和“frameOneChannel[i]=frameInt[4*i+1]*2**8+frameInt[4*i]#分离通道并在新列表中存储一个通道”之前添加该语句

  • *注意制表符空格

    哪一行引起错误?请注意,这是因为您正在访问的列表超出了它的长度。我建议,在询问更多问题之前,逐行调试代码并阅读@Pynchia在上面的评论上发布的内容。我真的不知道代码是如何工作的,所以我恐怕会重复在我把它发布到这里之前,不要把它搞砸了!错误出现在这一行:frameOneChannel[I]=frameInt[4*I+1]*2**8+frameInt[4*I]#单独的频道并将一个频道存储在新列表中感谢您的理解