python合併多個csv檔案並去重

右介發表於2017-06-22
#coding=utf-8
import os
import pandas as pd
import glob

def hebing():
    csv_list = glob.glob('*.csv')
    print(u'共發現%s個CSV檔案'% len(csv_list))
    print(u'正在處理............')
    for i in csv_list:
        fr = open(i,'r').read()
        with open('haha.csv','a') as f:
            f.write(fr)
    print(u'合併完畢!')

def quchong(file):
    df = pd.read_csv(file,header=0)
    datalist = df.drop_duplicates()
    datalist.to_csv(file)

if __name__ == '__main__':
    hebing()
    quchong("haha.csv")

 

相關文章