求助,python如何在csv插入一列的问题
展开全部
# -*- coding: utf8 -*-import csvl = [['1', 'Wonderful Spam'],['2', 'Lovely Spam']]#模拟数据写入一个csvwith open('eggs.csv', 'w', newline='') as csvfile: spamwriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) for row in l: spamwriter.writerow(row)#从文件读取l=[]with open('eggs.csv', newline='') as csvfile: spamreader = csv.reader(csvfile, delimiter=',', quotechar='"') for row in spamreader: l = l + [row]#把两列拼接增加为第三列写回到文件with open('eggs.csv', 'w', newline='') as csvfile: spamwriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) for row in l: print(row) spamwriter.writerow(row + [row[0]+row[1]])
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询