在python中连接两个文件时出现问题

2024-06-18 14:06:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在一个文件中附加两个ASCII文件(例如F1_Jan_01.txtF1_jan_01.txt,包括目录d01d02)。事实上,我有两个目录,其中我有每个类别的文件(F1F2F3),月和日(1到7),我想附加在两个不同目录中具有相同名称的文件。所以,我用Python编写了以下代码。你知道吗

import pandas as pd

maindir1="/home/d01/"
maindir2="/home/d02/"
outdir="/home/final/"

pol=[ "F1","F2","F3" ]

month=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]

for iis,ipol in enumerate(pol):
    for jjs,imonth in enumerate(month):
        for kk in range(1,7,1):
            df1 = pd.read_csv(maindir1+str(ipol)+"_"+str(imonth)+"_0"+str(kk)+".txt", sep="\t")
            df2 = pd.read_csv(maindir2+str(ipol)+"_"+str(imonth)+"_0"+str(kk)+".txt", sep="\t")
            df = pd.concat([ df1, df2 ], ignore_index=True)
            df.to_csv(outdir+str(ipol)+"_"+str(imonth)+"_0"+str(kk)+".txt",sep="\t",index=False)

问题是,在最终输出中,当它附加第二个文件时,它不会写入第一行。例如,第一个文件(在d01)有100000行,第二个文件(在d02)有50000行。因此,在最终输出中,正确写入前100000行,然后附加第二个文件的49000行(不包括第一行)。你知道吗

我需要在代码中定义其他内容吗?你知道吗


Tags: 文件csvin目录txthomeforf1
1条回答
网友
1楼 · 发布于 2024-06-18 14:06:21

如果不使用Pandas,下面是等效代码。(干编码,YMMV)

maindir1 = "/home/d01/"
maindir2 = "/home/d02/"
outdir = "/home/final/"

pols = ["F1", "F2", "F3"]
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

for ipol in pols:
    for imonth in months:
        for kk in range(1, 7):
            template_args = {"ipol": ipol, "imonth": imonth, "kk": kk}
            filename = "{ipol}_{imonth}_0{kk}.txt".format(ipol=ipol, imonth=imonth, kk=kk)
            out_name = os.path.join(outdir, filename)
            in_names = [os.path.join(maindir1, filename), os.path.join(maindir2, filename)]
            with open(out_name, "w") as out_file:
                for in_name in in_names:
                    with open(in_name, "r") as in_file:
                        out_file.write(in_file.read())

相关问题 更多 >