我有一长串以下表格清单---
a = [[1.2,'abc',3],[1.2,'werew',4],........,[1.4,'qew',2]]
Run Code Online (Sandbox Code Playgroud)
即列表中的值是不同的类型 - 浮点数,整数,字符串.如何将其写入csv文件,以便我的输出csv文件看起来像
1.2,abc,3
1.2,werew,4
.
.
.
1.4,qew,2
Run Code Online (Sandbox Code Playgroud) 我有一个example.csv包含内容的文件
1,"A towel,",1.0
42," it says, ",2.0
1337,is about the most ,-1
0,massively useful thing ,123
-2,an interstellar hitchhiker can have.,3
Run Code Online (Sandbox Code Playgroud)
我如何example.csv用Python 阅读?
同样,如果我有
data = [(1, "A towel,", 1.0),
(42, " it says, ", 2.0),
(1337, "is about the most ", -1),
(0, "massively useful thing ", 123),
(-2, "an interstellar hitchhiker can have.", 3)]
Run Code Online (Sandbox Code Playgroud)
如何data使用Python 写入CSV文件?
以前我用它来逐行将结果写入文本文件,在每一行中,值由制表符分隔
fd_out.write("%s\t%d\t%d\t%s\n" % (obj["name"],obj["count"],obj["age"],obj["params"]["country"])
Run Code Online (Sandbox Code Playgroud)
但现在我想要输出CSV输出,我想如何编写代码?就像这样或者我应该导入CSV并使用其他方法吗?对不起,我是编程的新手......
fd_out.write("%s,%d,%d,%s\n" % (obj["name"],obj["count"],obj["age"],obj["params"]["country"])
Run Code Online (Sandbox Code Playgroud)