将由空行分隔的许多csv连接成单个csv

时间:2018-04-19 17:43:41

标签: python csv

我有这个代码将许多csv从一个文件夹连接到一个csv文件,但我希望连接的csvs在最终的csv(finaloutput.csv)中用空行分隔。有任何想法吗?提前谢谢

#!/usr/bin/env python
#

import pandas as pd
import glob, os
import shutil


path = r'/Users/sonia/PaleoSkull/Animalsmanage/output'

allFiles = glob.glob(path + "/*.csv")
with open('finaloutput.csv', 'wb') as outfile:
    for i, fname in enumerate(allFiles):
        with open(fname, 'rb') as infile:
            if i != 0:
                infile.readline()  
            shutil.copyfileobj(infile, outfile)
            print(fname + " has been imported.")

1 个答案:

答案 0 :(得分:0)

with open('finaloutput.csv', 'wb') as outfile:
    for i, fname in enumerate(allFiles):
        with open(fname, 'rb') as infile:
            if i != 0:
                infile.readline()  
                outfile.write(b'\n')   # add a newline
            shutil.copyfileobj(infile, outfile)
            print(fname + " has been imported.")