Python 如何在合并文件之间添加空行

Python 如何在合并文件之间添加空行,python,pandas,Python,Pandas,我有几个CSV文件,我已经设法合并。但是,我需要在每个文件合并时在它们之间添加一个空行,以便我知道不同的文件从该点开始。什么都试过了。请帮忙 import os import glob import pandas def concatenate(indir="C:\\testing", outfile="C:\\done.csv"): os.chdir(indir) fileList=glob.glob("*.csv") dfList=[] colnames=[

我有几个CSV文件,我已经设法合并。但是,我需要在每个文件合并时在它们之间添加一个空行,以便我知道不同的文件从该点开始。什么都试过了。请帮忙

import os
import glob
import pandas

def concatenate(indir="C:\\testing", outfile="C:\\done.csv"):
    os.chdir(indir)
    fileList=glob.glob("*.csv")
    dfList=[]
    colnames=["Creation Date","Author","Tweet","Language","Location","Country","Continent"]
    for filename in fileList:
        print(filename)
        df=pandas.read_csv(filename, header=None)
        ins=df.insert(len(df),'\n')
        dfList.append(ins)

    concatDf=pandas.concat(dfList,axis=0)
    concatDf.columns=colnames
    concatDf.to_csv(outfile,index=None)

下面是一个示例脚本。您可以使用不存在键的方法来设置新行的值

最简单的解决方案似乎是创建一个模板
DataFrame
,用作分隔符,并根据需要设置值。然后只需将其插入数据帧列表中,以在适当的位置连接

最后,我删除了
chdir
,因为
glob
可以在任何路径中搜索

import glob
import pandas


def concatenate(input_dir, output_file_name):
    file_list=glob.glob(input_dir + "/*.csv")

    column_names=["Creation Date"
        , "Author"
        , "Tweet"
        , "Language"
        , "Location"
        , "Country"
        , "Continent"]

    # Create a separator template
    separator = pandas.DataFrame(columns=column_names)
    separator.loc[0] = [""]*7

    dataframes = []
    for file_name in file_list:
        print(file_name)
        if len(dataframes):
            # The list is not empty, so we need to add a separator
            dataframes.append(separator)
        dataframes.append(pandas.read_csv(file_name))

    concatenated = pandas.concat(dataframes, axis=0)
    concatenated.to_csv(output_file_name, index=None)
    print(concatenated)


concatenate("input", ".out.csv")

另一种甚至更短的方法是使用该方法以迭代方式构建连接的
数据帧


我用3个输入CSV文件测试了脚本:

input/1.csv

Creation Date,Author,Tweet,Language,Location,Country,Continent
2015-12-17,foo,Hello,EN,London,UK,Europe
2015-12-18,bar,Bye,EN,Manchester,UK,Europe
2015-12-28,baz,Hallo,DE,Frankfurt,Germany,Europe
Creation Date,Author,Tweet,Language,Location,Country,Continent
2016-01-09,bar,Tweeeeet,EN,New York,USA,America
2016-01-09,cat,Miau,FI,Helsinki,Finland,Europe
Creation Date,Author,Tweet,Language,Location,Country,Continent
2018-12-12,who,Hello,EN,Delhi,India,Asia
Creation Date,Author,Tweet,Language,Location,Country,Continent
2015-12-17,foo,Hello,EN,London,UK,Europe
2015-12-18,bar,Bye,EN,Manchester,UK,Europe
2015-12-28,baz,Hallo,DE,Frankfurt,Germany,Europe
,,,,,,
2016-01-09,bar,Tweeeeet,EN,New York,USA,America
2016-01-09,cat,Miau,FI,Helsinki,Finland,Europe
,,,,,,
2018-12-12,who,Hello,EN,Delhi,India,Asia
input/2.csv

Creation Date,Author,Tweet,Language,Location,Country,Continent
2015-12-17,foo,Hello,EN,London,UK,Europe
2015-12-18,bar,Bye,EN,Manchester,UK,Europe
2015-12-28,baz,Hallo,DE,Frankfurt,Germany,Europe
Creation Date,Author,Tweet,Language,Location,Country,Continent
2016-01-09,bar,Tweeeeet,EN,New York,USA,America
2016-01-09,cat,Miau,FI,Helsinki,Finland,Europe
Creation Date,Author,Tweet,Language,Location,Country,Continent
2018-12-12,who,Hello,EN,Delhi,India,Asia
Creation Date,Author,Tweet,Language,Location,Country,Continent
2015-12-17,foo,Hello,EN,London,UK,Europe
2015-12-18,bar,Bye,EN,Manchester,UK,Europe
2015-12-28,baz,Hallo,DE,Frankfurt,Germany,Europe
,,,,,,
2016-01-09,bar,Tweeeeet,EN,New York,USA,America
2016-01-09,cat,Miau,FI,Helsinki,Finland,Europe
,,,,,,
2018-12-12,who,Hello,EN,Delhi,India,Asia
input/3.csv

Creation Date,Author,Tweet,Language,Location,Country,Continent
2015-12-17,foo,Hello,EN,London,UK,Europe
2015-12-18,bar,Bye,EN,Manchester,UK,Europe
2015-12-28,baz,Hallo,DE,Frankfurt,Germany,Europe
Creation Date,Author,Tweet,Language,Location,Country,Continent
2016-01-09,bar,Tweeeeet,EN,New York,USA,America
2016-01-09,cat,Miau,FI,Helsinki,Finland,Europe
Creation Date,Author,Tweet,Language,Location,Country,Continent
2018-12-12,who,Hello,EN,Delhi,India,Asia
Creation Date,Author,Tweet,Language,Location,Country,Continent
2015-12-17,foo,Hello,EN,London,UK,Europe
2015-12-18,bar,Bye,EN,Manchester,UK,Europe
2015-12-28,baz,Hallo,DE,Frankfurt,Germany,Europe
,,,,,,
2016-01-09,bar,Tweeeeet,EN,New York,USA,America
2016-01-09,cat,Miau,FI,Helsinki,Finland,Europe
,,,,,,
2018-12-12,who,Hello,EN,Delhi,India,Asia

当我运行它时,以下输出被写入控制台:

控制台输出(使用concat)

较短变量的控制台输出略有不同(请注意第一列中的索引),但这对生成的CSV文件没有影响

控制台输出(使用附加)


最后,它生成的输出CSV文件是这样的:

out.csv

Creation Date,Author,Tweet,Language,Location,Country,Continent
2015-12-17,foo,Hello,EN,London,UK,Europe
2015-12-18,bar,Bye,EN,Manchester,UK,Europe
2015-12-28,baz,Hallo,DE,Frankfurt,Germany,Europe
Creation Date,Author,Tweet,Language,Location,Country,Continent
2016-01-09,bar,Tweeeeet,EN,New York,USA,America
2016-01-09,cat,Miau,FI,Helsinki,Finland,Europe
Creation Date,Author,Tweet,Language,Location,Country,Continent
2018-12-12,who,Hello,EN,Delhi,India,Asia
Creation Date,Author,Tweet,Language,Location,Country,Continent
2015-12-17,foo,Hello,EN,London,UK,Europe
2015-12-18,bar,Bye,EN,Manchester,UK,Europe
2015-12-28,baz,Hallo,DE,Frankfurt,Germany,Europe
,,,,,,
2016-01-09,bar,Tweeeeet,EN,New York,USA,America
2016-01-09,cat,Miau,FI,Helsinki,Finland,Europe
,,,,,,
2018-12-12,who,Hello,EN,Delhi,India,Asia

请确保您的代码格式正确,当前缩进被破坏,在Python中是一个显示停止符。我的代码工作正常。这只是我想补充的空白行。仅仅因为你认为缩进不正确,就意味着我投了反对票?不是因为我认为这是错误的,而是因为。第4行上的函数没有主体。从第5行开始,您丢失了一个缩进级别。也许只是在你写问题的时候,但这并不能改变它是错误的事实。因此,这个问题缺乏质量,对我来说,它表明你没有花足够的精力来写它。当这种情况发生变化时,我将很高兴地重新评估我的投票。