我正在尝试在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
中的数据。