我解析了一个CSV文件,当我尝试保存它时,出现错误“ TypeError:无法将字节连接到InMemoryUploadedFile”。
upload_obj = UploadField(csv=True)
csv_upload = request.FILES.get('uploads', None)
csv_parsed = csv.reader(codecs.iterdecode(csv_upload, 'utf-8'), delimiter=',')
[parse loop then...]
try:
upload_obj.save(request=request, content_list=csv_upload)
except Exception as e:
print('Error:', e)
尝试保存时出现错误。
答案 0 :(得分:2)
将InMemoryUploadedFile
转换为NamedTemporaryFile
,因此您可以将其视为普通文件:
file_temp = tempfile.NamedTemporaryFile()
file_temp.write(csv_upload.read())
print(file_temp.name) # This is the path.
完成后,只需执行file_temp.close()
即可将其删除。