csv 생성

1431 단어
이 두 개는 기초 함수입니다.
from django.utils.encoding import smart_str


def join_line(line):
    tmpl = ','.join([u'"{}"'] * len(line))
    return smart_str(tmpl.format(*line))


def response_csv(title, lines, encoding='utf-8'):
    from django import http
    import codecs
    if isinstance(title, unicode):
        title = title.encode(encoding)
    response = http.HttpResponse(codecs.BOM_UTF8 + ('\r
'.join(lines)), content_type='text/csv') response['Content-Disposition'] = 'attachment;filename="{}.csv"'.format(title) return response

인용하다
def make_lines_day_all(values):
    lines = []
    for v in values:
        date = v.get('date')
        print date
        register_num = v.get('register_num')
        income = '{:.2f}'.format(v.get('income') * 0.01)
        join_num = v.get('join_num')
        visit_num = v.get('visit_num')
        line = [date, register_num, income, visit_num, join_num]
        lines.append(join_line(line))
    return lines


def export_day_all(request):
    query = BizStatistics.objects.values().all()
    print query
    title = ' '
    lines = [join_line((u' ', u' ', u' ', u' ', u' '))]
    lines += make_lines_day_all(query)
    return response_csv(title, lines)

좋은 웹페이지 즐겨찾기