我正在尝试将列表写入 CSV。每个列表都是一个字符串,一行中的每一列都带有括号。到目前为止,这是我尝试过的:
from csv import writer
new_csv_file = "new sheet.csv"
a_list_1 = [1, 2, 3, 4, 5, 6, "ab", 8, 9]
a_list_2 = ["cd", 2, 3, 4, 5, 6, "ab", 8, 9]
with open(new_csv_file, 'a', newline="") as fd:
csv_writer = writer(fd, delimiter=',')
for i in [a_list, a_list_2]:
csv_writer.writerow([i])
fd.write("\n")
这就是上面的代码给我的:
这就是我想要的方式,在末尾有一个新行,用于在下一个循环的下一行中以相同的方式追加更多列表:
我该怎么做?
尝试删除 for 循环:
from csv import writer
new_csv_file = "new sheet.csv"
a_list_1 = [1, 2, 3, 4, 5, 6, "ab", 8, 9]
a_list_2 = ["cd", 2, 3, 4, 5, 6, "ab", 8, 9]
with open(new_csv_file, 'a', newline="") as fd:
csv_writer = writer(fd, delimiter=',')
csv_writer.writerow([a_list, a_list_2])
fd.write("\n")
我是一名优秀的程序员,十分优秀!