简体   繁体   English

多个 Write 语句以在 csv 文件中获取输出

[英]Multiple Write statements to get output in csv file

I have multiple for and write statements in my code , i an looking to get the output of each write statement in different rows in the csv file我的代码中有多个 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'])

Result : All in one row结果:全部在一行中

ae
"2b30c894e9bf42bc9033c7"
"fdb7911f39014ea682c7329"
aeims
"b5b54ae564ba"
"b788837b21ed4"

Expected预期的

Row 1        Row 2
ae           "2b30c894e9bf42bc9033c7"
             "fdb7911f39014ea682c7329"
aeims        "b5b54ae564ba"
             "b788837b21ed4"

I would think of trying to break it up into "cells".我会考虑尝试将其分解为“细胞”。 Remember a csv has a comma after each input and then you can get to the next row by the \\ni believe it is.请记住,csv 在每个输入后都有一个逗号,然后您可以通过 \\ni 进入下一行。 So, create a function to make each individual cell and call that inside your loops.因此,创建一个函数来制作每个单独的单元格并在循环中调用它。 Key tip, try to only open and close the csv as least often as possible, can run into errors with windows not being able to keep up.关键提示,尝试仅尽可能少地打开和关闭 csv,可能会遇到 Windows 无法跟上的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM