Python Django:在我的内存中导出excel文件
我想在内存中导出一些Excel文件,这是我的代码Python Django:在我的内存中导出excel文件,python,django,Python,Django,我想在内存中导出一些Excel文件,这是我的代码 wrapper = open('/home/filename.xlsx') response = HttpResponse(wrapper,content_type='application/force-download') response['Content-Disposition'] = 'attachment ; filename=%s' % smart_str('filename.xlsx') response['X-Sendfile']
wrapper = open('/home/filename.xlsx')
response = HttpResponse(wrapper,content_type='application/force-download')
response['Content-Disposition'] = 'attachment ; filename=%s' % smart_str('filename.xlsx')
response['X-Sendfile'] = smart_str('/home/filename.xlsx')
return response
使用Python2.7时没有问题,但如果使用Python3.5,则会出现错误
'utf-8' codec can't decode byte 0x9c in position 10:invalid start byte
我猜错误是因为它们的不同(开放函数)
在我的内存中打开excel文件并将其导出的适当方式是什么?在Python 3.x中,默认情况下,文件以文本模式打开。将第一行更改为:
wrapper = open('/home/filename.xlsx', 'rb')
来自(我的):
通常,文件是以文本模式打开的,也就是说,您可以从文件中读取和写入字符串,这些字符串以特定的编码进行编码。如果未指定编码,则默认值取决于平台(请参阅open())。'附加到模式的b'以二进制模式打开文件:现在数据以字节对象的形式读取和写入此模式适用于所有不包含文本的文件。