Python csv文件纵向合并

图片名称

  工程师朋友让我帮忙改一下Python脚本,业务场景是有现有两份csv文件,一张fcsv存放原始数据,一张fcsv_new存放更新数据,现在需要把更新的数据追加到原始表中

  Python csv纵向合并程序如下:

import csv

fcsv="d:\\test\\20180323HF.csv" # 定义文件路径
fcsv_new="d:\\test\\20180323HFnew.csv"
reader_new = csv.DictReader(open(fcsv_new,encoding="utf-8")) # 读取fscv_new的数据
header =  reader_new.fieldnames # 读取fscv_new的字段名
with open(fcsv,'a',encoding="utf-8") as csvfile: 
    writer = csv.DictWriter(csvfile, fieldnames=header)
    writer.writerows(reader_new) # 将fcsv_new写入fcsv

reader = csv.DictReader(open(fcsv,encoding="utf-8")) #读取合并后的fcsv
for row in reader: 
    print(row)

分类: Python技术