Python 2至3移植,将字节与字符串进行比较

时间:2018-10-03 12:54:39

标签: python string type-conversion byte python-2to3

我发现一个'swmixer' sound module似乎很适合我的项目。不幸的是,它是用Python 2编写的,而我的项目是Python3。我试图移植它,但是这段代码给我带来了麻烦。

        data = []
        r = ' '
        while r != '':
            r = wf.readframes(4096)
            data.append(r)

可以在here的第275行找到整个代码“ swmixer.py”。

我认为问题是 r 是字节,并且正在与''字符串进行比较。这在Python 3中成为问题。由于r永远不等于”,因此它永远卡在while循环中。我已经尝试了很多事情,但是我不知道如何正确进行转换。

我尝试播放.wav文件的代码是:

import swmixer
import time

swmixer.init(samplerate=44100, chunksize=1024, stereo=False)
swmixer.start()
snd = swmixer.Sound("hold.wav")
snd.play()
time.sleep(2.0) #don't quit before we hear the sound!

有什么建议吗?谢谢!

0 个答案:

没有答案