gpt4 book ai didi

Python - 将元组列表写入 CSV 文件,忽略括号内的逗号

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:00 24 4
gpt4 key购买 nike

所以我有以下列表: L2 = [(1,2), (1,3), (1,4), (1,5)]

我想将其写入一列四行的 csv 文件:

(1,2)
(1,3)
(1,4)
(1,5)

但是我在这里的精彩尝试

with open('myfile.csv', "w") as outfile:
for entries in L2:
outfile.write(str(entries))
outfile.write("\n")

引导我到两列和四行:

(1   2)
(1 3)
(1 4)
(1 5)

最佳答案

您可以循环遍历项目并使用字符串格式:

with open('myfile.csv', "w") as outfile:
for a, b in L2:
outfile.write("({},{})\n".format(a, b))

关于Python - 将元组列表写入 CSV 文件,忽略括号内的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47212518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com