类型错误:强制到 Unicode:需要字符串或缓冲区,_io.找到字节IO



我正在尝试一周如何从烧瓶 POST 选项中将输入文件提供给 Textract。

@app.route('/input', methods=['POST'])
def input():
    request_file = request.files.get('file')
    r = textract.process(io.BytesIO(request_file.read()))
    return r 

上面的代码给我抛出错误

类型错误:强制到 Unicode:需要字符串或缓冲区,_io。字节IO 发现

我尝试了send_file一个小测试,以检查它是否真的需要输入并检查 BytesIO 在我的情况下是否运行良好:

@app.route('/input', methods=['POST'])
def input():
    request_file = request.files.get('file')
    return send_file(io.BytesIO(request_file.read()),attachment_filename=
request_file.filename)

上面的代码适用于PDF文件并发送响应(下载PDF文件(。当我尝试.docx,.txt文件时,它在屏幕上显示一些奇怪的输出:PK

我的问题,我现在如何将此io.bytes(request_file.read())作为文件发送到 Textract?我试图到处寻找答案,但我做不到。

我现在应该解码或编码吗?

textract.process()需要一个字符串,但你发送io.BytesIO(request_file.read())。我不太确定你为什么要使用io.BytesIO.你能试试吗:

textract.process(request_file.read())

最新更新