手动添加行到StreamingHttpResponse(Django)

n53p2ov0  于 2023-01-06  发布在  Go
关注(0)|答案(3)|浏览(111)

我正在使用Django的StreamingHttpResponse动态地流式传输一个大的CSV文件,根据文档,一个迭代器被传递给响应的streaming_content参数:

import csv
from django.http import StreamingHttpResponse

def get_headers():
    return ['field1', 'field2', 'field3']

def get_data(item):
    return {
        'field1': item.field1,
        'field2': item.field2,
        'field3': item.field3,
    }

# StreamingHttpResponse requires a File-like class that has a 'write' method
class Echo(object):
    def write(self, value):
        return value

def get_response(queryset):
    writer = csv.DictWriter(Echo(), fieldnames=get_headers())
    writer.writeheader() # this line does not work

    response = StreamingHttpResponse(
        # the iterator
        streaming_content=(writer.writerow(get_data(item)) for item in queryset),
        content_type='text/csv',
    )
    response['Content-Disposition'] = 'attachment;filename=items.csv'

    return response

我的问题是:如何在CSV编写器上手动写入行?手动调用writer. writerow(data)或writer. writeader()(也在内部调用writerow())似乎不会写入数据集,而是仅将streaming_content生成/流传输的数据写入输出数据集。

yh2wf1be

yh2wf1be1#

答案是使用生成器函数生成结果,而不是动态计算结果(在StreamingHttpResponse的streaming_content参数内),并使用我们创建的伪缓冲区(Echo类)向响应写入一行:

import csv
from django.http import StreamingHttpResponse

def get_headers():
    return ['field1', 'field2', 'field3']

def get_data(item):
    return {
        'field1': item.field1,
        'field2': item.field2,
        'field3': item.field3,
    }

# StreamingHttpResponse requires a File-like class that has a 'write' method
class Echo(object):
    def write(self, value):
        return value

def iter_items(items, pseudo_buffer):
    writer = csv.DictWriter(pseudo_buffer, fieldnames=get_headers())
    yield pseudo_buffer.write(get_headers())

    for item in items:
        yield writer.writerow(get_data(item))

def get_response(queryset):
    response = StreamingHttpResponse(
        streaming_content=(iter_items(queryset, Echo())),
        content_type='text/csv',
    )
    response['Content-Disposition'] = 'attachment;filename=items.csv'
    return response
cedebl8k

cedebl8k2#

建议的解决方案实际上可能会导致不正确/不匹配的CSV(标题与数据不匹配)。您可能希望将受影响的部分替换为以下内容:

header = dict(zip(fieldnames, fieldnames))
yield writer.writerow(header)

这是来自writeheader www.example.com的实现https://github.com/python/cpython/blob/08045391a7aa87d4fbd3e8ef4c852c2fa4e81a8a/Lib/csv.py#L141:L143
由于某种原因,它在yield中表现不佳
希望这对以后的人有帮助:)
还请注意,如果使用python 3.8+,则无需修复,因为此PR:https://bugs.python.org/issue27497

58wvjzkj

58wvjzkj3#

你可以在python中使用itertools链接生成器,将标题行添加到查询集行
下面是您操作方法:

import itertools

def some_streaming_csv_view(request):
    """A view that streams a large CSV file."""
    # Generate a sequence of rows. The range is based on the maximum number of
    # rows that can be handled by a single sheet in most spreadsheet
    # applications.
    headers = [["title 1", "title 2"], ]
    row_titles = (header for header in headers) # title generator

    items = Item.objects.all()
    rows = (["Row {}".format(item.pk), str(item.pk)] for item in items)
    pseudo_buffer = Echo()
    writer = csv.writer(pseudo_buffer)
    rows = itertools.chain(row_titles, rows)  # merge 2 generators
    return StreamingHttpResponse(
        (writer.writerow(row) for row in rows),
        content_type="text/csv",
        headers={'Content-Disposition': 'attachment; filename="somefilename.csv"'},
    )

你会得到一个带有标题和查询集的csv文件:

title 1, title 2
1, 1
2, 2
...

相关问题