首页 > 解决方案 > 需要帮助解码 WAV 文件电子邮件附件

问题描述

我正在将 Google App Engine 用于基础架构,并且我正在尝试解码附加的 .WAV 文件并将其解码为字符串以传递给谷歌云的语音到文本 api。

这是我的代码,我不知道该怎么做。我尝试使用“base64”或“有效负载中的编码属性”进行解码,但我不断收到此错误:“UnicodeDecodeError: 'utf8' codec can't decode byte 0xc6 in position 4: invalid continuation byte”

我用于测试的 .WAV 文件已使用在线 wav 到 base64 解码器正确解码,但由于某种原因,它不适用于此代码。

def receive(self, mail_message):

    if hasattr(mail_message, 'attachments'):
        file_name = ""
        file_contents = ""
        for filename, filecontents in mail_message.attachments:
            file_name = filename
            file_contents = filecontents.payload.decode(filecontents.encoding)

标签: pythongoogle-app-enginedecodewavgoogle-app-engine-python

解决方案


filecontents不需要任何解码:它已经采用正确的格式(编码为)base64并准备好使用语音到文本 api 进行进一步处理(如评论部分所述)。


推荐阅读