在python中将嵌套列表转换为.csv的更有效方法 [英] More efficient way to convert a nested list to .csv in python
本文介绍了在python中将嵌套列表转换为.csv的更有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我下面有一个像Python_List
这样的嵌套列表,我想像下面这样制作一个.csv
:
I've a nested list like Python_List
below and I want to make a .csv
like below:
Python_List|-> .csv
[['2','4'],| 2,4
['6','7'],| 6,7
['5','9'],| 5,9
['4','7']]| 4,7
到目前为止,我正在使用以下代码:
So far I'm using this code:
Python_List=[['2','4'], ['6','7'], ['5','9'], ['4','7']]
with open('test.csv','w') as f:
for i in range(0,len(Python_List)):
f.write('%s,%s\n' %(Python_List[i][0],Python_List[i][1]))
还有其他更有效的选择吗?
Are there any alternatives more efficient?
推荐答案
It may not be more efficient but it will be easier to understand.
例如
import csv
with open('test.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',')
csvwriter.writerows(Python_List)
这篇关于在python中将嵌套列表转换为.csv的更有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文