求助,python如何在csv插入一列的问题
2个回答
2017-07-28 · 知道合伙人互联网行家
关注
展开全部
# -*- coding: utf8 -*-
import csv
l = [['1', 'Wonderful Spam'],['2', 'Lovely Spam']]
#模拟数据写入一个csv
with 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]])
import csv
l = [['1', 'Wonderful Spam'],['2', 'Lovely Spam']]
#模拟数据写入一个csv
with 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]])
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询