[英]How do I write a list to a csv and make each element in the list be its own column?
Say I have a list in python like so: 假设我在python中有一个列表,如下所示:
my_list = ['35.32', '23.12', '34.34']
I want to be able to do something like so: 我希望能够做这样的事情:
writerposts.writerow(["cat", "meow", my_list])
The above puts all the values in my_list into a single column though with the square brackets representing the list still there. 上面的代码将my_list中的所有值放在一列中,尽管方括号仍然表示列表。 So the csv looks like: 因此,csv看起来像:
cat meow ['35.32', '23.12', '34.34']
where the parts in between the square brackets are all in one column 方括号之间的部分都在一栏中
but i want it to look like with the floating numbers all in their own columns: 但我希望它看起来像都是在自己的列中都包含浮点数:
cat meow 35.32 23.12 34.34
This seems so simple but i can't find anyone online explaining how to do this - thoughts?? 这似乎很简单,但我找不到在线解释此操作的任何人-思想?
If you are on Python 3 you can use sequence unpacking: 如果您使用的是Python 3,则可以使用序列解压缩:
writerposts.writerow(["cat", "meow", *my_list])
Or a more backwards compatible solution: 或更向后兼容的解决方案:
writerposts.writerow(["cat", "meow"] + my_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.