我的代码中有多个for和write语句,我希望在csv文件的不同行中获取每个write语句的输出
with open("csvtest_new.csv", "w") as f:
writer = csv.writer(f,delimiter='\t', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for namespace in namespaces:
writer.writerow(namespace['name'])
x = client.bucket.list(namespace['name'])
bucketlist = x['object_bucket']
for bucket in bucketlist:
writer.writerow(bucket['name'])
结果:全部排成一列
ae
"2b30c894e9bf42bc9033c7"
"fdb7911f39014ea682c7329"
aeims
"b5b54ae564ba"
"b788837b21ed4"
预期
Row 1 Row 2
ae "2b30c894e9bf42bc9033c7"
"fdb7911f39014ea682c7329"
aeims "b5b54ae564ba"
"b788837b21ed4"
答案 0 :(得分:-1)
我会考虑尝试将其分解为“单元”。请记住,CSV每次输入后都有一个逗号,然后您可以按\ n转到下一行,我相信是这样。因此,创建一个函数来制作每个单独的单元格并在循环中调用它。关键提示,尝试仅尽可能最少地打开和关闭csv,可能会因Windows无法跟上而出错。