指令开放源代码:Python索引器错误:列表索引超出范围

2024-05-03 22:14:05 发布

您现在位置: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 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()

非常感谢, 李斯特


Tags: andofthetoimport错误filenamewave
1条回答
网友
1楼 · 发布于 2024-05-03 22:14:05

学习-检查这些可能有帮助:

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

*监视选项卡空间

相关问题 更多 >