试图动态生成流

时间:2019-07-25 14:29:35

标签: python python-3.x stream byte

我正在尝试在python 3.6中动态生成csv流


def csv_fixture(rows, type):
    headers = None
    if type == "merchant_upload":
        headers = MerchantCSV.ordered_columns()
    elif type == "invoice_upload":
        headers = InvoiceCSV.ordered_columns()
    assert headers is not None
    rows = [headers] + rows
    output = io.BytesIO()
    writer = csv.writer(io.TextIOWrapper(output))
    for row in rows:
        writer.writerow([s.encode("utf-8") for s in row])
    return output

csv_fixture([['a','b'], ['c','d']], "merchant_upload")

当我使用调试器查看output.readline()时,我得到一个空字符串:(

我不知道为什么会这样。我的预期输出是一个io.Bytes对象,其中包含rows中的数据。

0 个答案:

没有答案