如何将列表列表写入CSV文件Python? [英] How to write list of lists to CSV file Python?
本文介绍了如何将列表列表写入CSV文件Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表,如[('a', 'b', 'c'), ('d', 'e', 'f'),....]
.我想将其写入这样的CSV文件中-
I have a list of lists like [('a', 'b', 'c'), ('d', 'e', 'f'),....]
. I want to write it to a CSV file like this-
a, b, c
d, e, f
我该怎么做?
我尝试使用csv.writerows,但是输出文件中的每个字符都位于不同的单元格中,并且所有字符都位于同一行中.从某种意义上说,第一行中的单元格具有'a''b'等.
I've tried using csv.writerows but the output file had each character in different cells and all together in same row. In the sense, the cells in row one had ' a ' ' b ' etc.
谢谢.
推荐答案
如果您有Pandas,则非常简单快捷.我假设您有一个称为数据"的元组列表.
If you have Pandas, it's pretty easy and fast. I assume you have a list of tuples called "data".
import pandas as pd
data = [('a', 'b', 'c'), ('d', 'e', 'f')]
df = pd.DataFrame(data)
df.to_csv('test.csv', index=False, header=False)
完成!
列表列表"->元组列表"
"list of lists" -> "list of tuples"
这篇关于如何将列表列表写入CSV文件Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文