How to write List of lists in csv file in python

后端 未结 2 561
猫巷女王i
猫巷女王i 2020-12-03 15:09

I have a list of lists and I want to write it in csv file Example list:

data=[[\'serial\', \'name\', \'subject\'],[\'1\', \'atul\',\'tpa\'],[\'2\', \'carl\',         


        
2条回答
  •  不思量自难忘°
    2020-12-03 15:55

    This is trivial with the csv module:

    with open('output.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerows(data)
    

    You already have the header in data as the first row; you can write all rows in one go with the writer.writerows() method. That's all there is to it, really.

提交回复
热议问题