django

Django-26 django生成csv文件

2021-08-02  本文已影响0人  JuliusL

Python提供了内建库 - csv;可直接通过该库操作csv文件
案例如下:

import csv
with open('eggs.csv','w',newline='') as csvfile:
  writer = csv.writer(csvfile)
  writer.writerow(['a','b','c'])

csv文件下载

在网站中,实现下载csv,注意如下:

import csv
from django.http import HttpResponse

def make_csv_view(request):
  response = HttpResponse(content_type='text/csv')
  response['Content-Disposition'] = 'attachment;filename="test.csv"'
  all_data = ['a','b','c','d']
  writer = csv.writer(response)
  writer.writerow(all_data)
  return response  
上一篇下一篇

猜你喜欢

热点阅读