在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)