我正在django中创建一个CSV文件,当您访问一个url时它会下载CSV文件,它的工作原理很棒,唯一的好处是我希望在写入文件时将其存储在内存中而不是硬盘中.如何使用导入的csv执行此操作
这是使用列表创建CSV的Django的
def data_Feed_file(request):
open_publications = self.get_publications(user_context)
with open('facebook_Feed.csv','wb') as csvfile:
filewriter = csv.writer(csvfile,delimiter=',',quotechar='|',quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['id','title','description'])
for op_pub in open_publications:
filewriter.writerow([op_pub.id,op_pub.name,str(op_pub.description)])
with open('facebook_Feed.csv','rb') as myfile:
response = HttpResponse(myfile,content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=facebook_Feed.csv'
return response
最佳答案