You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

零基础求助:运行2012年Python音频代码出现TypeError错误

Python2旧代码适配Python3的修复方案

问题背景

从未编写过代码,运行一段2012年的Python音频处理代码时触发错误,错误信息如下:

Traceback (most recent call last):
File "C:\Users\dabea\Downloads\3DPrintedRecord-master\wavtotext.py", line 29, in
frameInt = list(map(ord, list(frame)))#turn into array
TypeError: ord() expected string of length 1, but int found

原代码内容:

import wave
import math
import struct

bitDepth = 8#target bitDepth
frate = 44100#target frame rate

fileName = "C:/Users/dabea/desktop/Spendtime Palace __ Sonora (OFFICIAL VIDEO) (320 kbps).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 = list(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()

错误原因

这段代码是为Python2编写的,在Python3环境下,wave.readframes()返回的是bytes对象——直接把bytes转成list,每个元素本身就是0-255的整数。但原代码里用了ord()函数,这个函数是用来把长度为1的字符串转成ASCII码的,现在输入的是整数,自然就报错了。

修复后的代码

直接删掉map(ord, ...)的处理,因为list(frame)已经是整数数组了,同时还修复了原代码里的文件名截取bug:

import wave
import math
import struct

bitDepth = 8  # 目标位深
frate = 44100  # 目标帧率

fileName = "C:/Users/dabea/desktop/Spendtime Palace __ Sonora (OFFICIAL VIDEO) (320 kbps).wav"  # 要导入的文件路径(可修改)

# 读取文件数据
w = wave.open(fileName, 'r')
numframes = w.getnframes()

frame = w.readframes(numframes)

# 直接将bytes转为整数列表,无需ord处理(Python3特性)
frameInt = list(frame)

# 分离左右声道并合并字节
frameOneChannel = [0] * numframes  # 初始化单声道数据列表
for i in range(numframes):
    frameOneChannel[i] = frameInt[4*i+1] * 2**8 + frameInt[4*i]
    if frameOneChannel[i] > 2**15:
        frameOneChannel[i] -= 2**16
    elif frameOneChannel[i] == 2**15:
        frameOneChannel[i] = 0

# 转换为带逗号分隔的字符串
audioStr = ",".join(map(str, frameOneChannel)) + ","

# 生成输出文件名并写入文件
fileName = fileName[:-4]  # 正确移除.wav后缀(原代码[:-3]会出错)
with open(fileName + ".txt", "w") as text_file:
    text_file.write(audioStr)

额外说明

  1. 原代码里fileName[:-3]是错误的,因为.wav是4个字符,用[:-4]才能正确去掉后缀,避免生成类似xxxv.txt的错误文件名。
  2. 用","join(...)代替循环拼接字符串,处理大文件时速度会快很多。
  3. with语句会自动关闭文件,比手动调用close()更安全。

内容的提问来源于stack exchange,提问作者helpmelol

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 08:45:31