我无法弄清楚如何将此列表写入 csv 文件,python

I cant figure out how to write this list to a csv file how i want it, python

这是我的代码:

geometries = [[a, b, c], [d, e, f]] #Note I have lists within a list, this is required

with open("./datafiles/" + name, 'wb') as csvfile:
        writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
        writer.writerow(geometries)

此代码旨在将几何中的列表写入 CSV 文件。 CSV 文件应如下所示:

a, b, c

d, e, f

相反,我得到了一个如下所示的 txt 文件:

|['a', 'b', 'c']|,|['d','e','f']| 

如何从文本文件中删除这些 ( | [ ] '' ) 并使其如上显示?

您想使用 .writerows() 而不是 writerow()

通过使用正确的函数,它会按照您的预期输出您的 CSV 文件。

在某些编辑器中,行显示在同一行的问题是由于您的 lineterminator。您将其设置为 \n,而不是默认的 \r\n

.writerow() expects a flat list as a parameter. To write a list of rows at once use .writerows():

geometries = [[a, b, c], [d, e, f]] 

with open("./datafiles/" + name, 'wb') as csvfile:
    writer = csv.writer(csvfile, delimiter=',', quotechar='|',
                        quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
    writer.writerows(geometries)