python把wav本地转文字_python-将WAV字节从POST数据保存到磁盘(cherryp...

本文描述了一个使用CherryPy框架在Python中处理WAV文件上传到本地磁盘时遇到的问题。作者在尝试保存从POST数据接收的WAV字节时遇到了'Part'类型的错误,需要将数据转换为可写入文件的格式。更新中提到了尝试保存原始数据和使用`raw.file.read()`的方法,但都遇到了相同错误。
摘要由CSDN通过智能技术生成

我正在使用cherrypy将WAV文件从浏览器保存到本地磁盘.传入:

{'audio_file': [, ], 'user_data': u'{"id":"1255733204",'audio_length': [u'10.03102', u'22.012517', u'22.012517']}

我收到此错误:

try:

f = open('/audiostory/'+filename,'wb')

logging.debug( '[SAVEAUDIO] tried to write all of audio_file input at once' )

f.write(kw.get('audio_file'))

f.close()

logging.debug( ('saved media file %s to /audiostory/' % f.name) )

except Exception as e:

logging.debug( ('saved media NOT saved %s because %s' % (f.name,str(e))) )

"Must be convertible to a buffer, not Part."

那么我该如何处理这类数据,即将其转换为’.Part’但它应该是原始的WAV数据?我是否缺少标题或其他内容?

更新

杰森(Jason)-您会看到我故意没有发送任何标头或其他信息,因为我想看看小巧的东西会带来原始效果.这是cherrypy site.py文件:

@cherrypy.expose

def saveaudio(self, *args, **kw):

import audiosave

return audiosave.raw_audio(kw.get('audio_file'))

以及audiosave中的功能:

def raw_audio(raw):

""" is fed a raw stream of data (I think) and saves it to disk, with logging. """

import json

import logging

LOG_FILENAME = 'error.log'

logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)

logging.debug( '[SAVEAUDIO(raw)]'+str(raw) )

filename = assign_filename()

try:

#SAVE FILE TO DISK /s/audiostory/

f = open('/home/djotjog/webapps/s/audiostory/'+filename,'wb')

logging.debug( '[SAVEAUDIO] tried to write all of audio_file input at once' )

f.write(raw)

f.close()

logging.debug( ('media SAVED %s' % f.name) )

except Exception as e:

logging.debug( ('media NOT saved %s because %s' % (f.name,str(e))) )

return json.dumps({"result":"414","message":"ERROR: Error saving Media file "+f.name})

return raw

我也尝试了f.write(raw.file.read()),但发生相同的错误.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值