readList = [] #用来装合并文件的内容
titleList = ["date","district","region"] #文件标题
for i in listName:
#i 是文件名
#############读取多个csv文件内容###################
with open('{}.csv'.format(i),'r',newline="",encoding="GB18030") as read_csvfile:
readcsv_all = csv.reader(read_csvfile)
next(read_csvfile) #跳过第一行“标题”
for line in readcsv_all:
line.append(i) #添加“文件名”作为新的列
readList.append(line)
############写入文件##############################
with open('alldata.csv', 'a+' , newline = "" , encoding = "GB18030") as f:
csv_write = csv.writer(f)
csv_write.writerow(titleList) #写入文件标题
for line in readList:
# print(line)
csv_write.writerow(line)