如何使用python将头文件与数据文件结合起来?

如何使用python将头文件与数据文件结合起来?,python,Python,我有单独的文件,其中一部分是仅包含标题信息的文件,如下图所示: ~“header1.txt”的内容~ ~“header2.txt”的内容~ ~“header3.txt”的内容~ 另一部分是仅包含数据的文件,如下所示: ~“data1.txt”的内容~ ~“data2.txt”的内容~ ~“data3.txt”的内容~ 组合对应的数据文件后,结果与下面的示例相似: ~“asc1.txt”的内容~ ~“asc2.txt”的内容~ ~“asc3.txt”的内容~ 在用python编写这篇文章时,谁能给

我有单独的文件,其中一部分是仅包含标题信息的文件,如下图所示: ~“header1.txt”的内容~

~“header2.txt”的内容~

~“header3.txt”的内容~

另一部分是仅包含数据的文件,如下所示:

~“data1.txt”的内容~

~“data2.txt”的内容~

~“data3.txt”的内容~

组合对应的数据文件后,结果与下面的示例相似:

~“asc1.txt”的内容~

~“asc2.txt”的内容~

~“asc3.txt”的内容~

在用python编写这篇文章时,谁能给我一些帮助?谢谢

试试这个(用python 3.4编写)。很长,但应该更容易理解:

# can start by creating a function to read contents of
# each file and return the contents as a string
def readFile(file):
    contentsStr = ''
    for line in file:
        contentsStr += line
    return contentsStr

# Read all the header files header1, header2, header3
header1 = open('header1.txt','r')
header2 = open('header2.txt','r')
header3 = open('header3.txt','r')

# Read all the data files data1, data2, data3
data1 = open('data1.txt','r')
data2 = open('data2.txt','r')
data3 = open('data3.txt','r')

# Open/create output files asc1, asc2, asc3
asc1_outFile = open('asc1.txt','w')
asc2_outFile = open('asc2.txt','w')
asc3_outFile = open('asc3.txt','w')

# read contents of each header file and data file into string variabls
header1_contents = readFile(header1)
header2_contents = readFile(header2)
header3_contents = readFile(header3)

data1_contents = readFile(data1)
data2_contents = readFile(data2)
data3_contents = readFile(data3)

# Append the contents of each data file contents to its
# corresponding header file
asc1_contents = header1_contents + '\n' + data1_contents
asc2_contents = header2_contents + '\n' + data2_contents
asc3_contents = header3_contents + '\n' + data3_contents

# now write the necessary results to asc1.txt, asc2.txt, and
# asc3.txt output files respectively
asc1_outFile.write(asc1_contents)
asc2_outFile.write(asc2_contents)
asc3_outFile.write(asc3_contents)

# close the file streams
header1.close()
header2.close()
header3.close()
data1.close()
data2.close()
data3.close()
asc1_outFile.close()
asc2_outFile.close()
asc3_outFile.close()

# done!

顺便说一下,确保头文件和数据文件与python脚本位于同一目录中。否则,您可以在上面的代码中相应地编辑这些文件的文件路径。输出文件asc1.txt、asc2.txt和asc3.txt将在与python源文件相同的目录中创建。

如果头文件的数量等于数据文件的数量,则此操作有效

#Glob is imported to get file names matching to the given pattern 

import glob
header=[]
data=[]

#Traversing through the file and getting the content

for files1 in glob.glob("directory/header*.txt"):
    a=open(files1,"r").read()
    header.append(a)

for files2 in glob.glob("directory/data*.txt"):
    a1=open(files2,"r").read()
    data.append(a1)

#Writng the content into the file

for i in range(1,len(data)+1):
    writer=open("directory/asc"+str(i)+".txt","w")
    writer.write(header[i-1]+"\n\n"+data[i-1])
    writer.close()
编辑

只有当它们位于不同的文件夹中,并且该文件夹中除了头文件或数据文件之外不应有其他文件时,此方法才有效

#Glob is imported to get file names matching to the given pattern 

import glob
header=[]
data=[]

#Traversing through the file and getting the content

for files1 in glob.glob("directory1/*.txt"):
    a=open(files1,"r").read()
    header.append(a)

for files2 in glob.glob("directory2/*.txt"):
    a1=open(files2,"r").read()
    data.append(a1)

#Writng the content into the file

for i in range(1,len(data)+1):
    writer=open("directory/asc"+str(i)+".txt","w")
    writer.write(header[i-1]+"\n\n"+data[i-1])
    writer.close()

如果您真的想在Python中使用它,下面是一种方法

for i in range(1,4):

    h = open('header{0}.txt'.format(i),'r')
    d = open('data{0}.txt'.format(i),'r')
    a = open('asc{0}.txt'.format(i),'a')

    hdata = h.readlines()
    ddata = d.readlines()
    a.writelines(hdata+ddata)
    a.close()

当然,假设两个文件的数量都是3,并且都遵循您使用的相同命名约定。

这只需要一个命令:
cat header$i.txt data$i.txt>asc$i.txt
它们都在同一文件夹中吗谢谢,这非常有用。但实际上是一堆头文件和数据文件,它们并没有像这里显示的文件名那样系统地命名。那么,如果有什么好方法可以自动添加文件,而不是手动添加?比如使用循环过程等等。@King Zhao:可以使用循环自动添加文件。事实上,下面的其他答案也证明了这一点。但是,由于您正试图处理不规则文件名的文件,因此在某些情况下,您将需要一种方法将每个数据文件映射到其相应的头文件。您可以在每次运行时使用程序读取的配置文件。配置文件可以是一个简单的文本文件,其中包含头文件及其相应数据文件的名称。这样,即使文件名不规则,您的程序也可以组合正确的头文件和数据文件。非常感谢。但实际上我有一堆单独的头文件和数据文件,它们没有系统地命名为这里显示的文件名。所以,如果有什么好方法可以添加名称不规则的文件?当然,它们是分开的。失败了,需要15个信誉。非常感谢。但实际上我有一堆单独的头文件和数据文件,它们没有系统地命名为这里显示的文件名。因此,如果有任何好的方法来添加名称不规则的文件?这些头文件和数据文件有多少?…如果数字不是很大,比如文件名的字典,{head1:data1,head2:data2…等},那么我们可以将它们添加到一起。
11 21 31 41
21 24 12 23
21 22 11 31
10 26 14 33
a 3
b 2
c 4

10 20 30 40
20 14 22 33
a 4
b 3
c 5

11 21 31 41
21 24 12 23
a 1
b 7
c 6

21 22 11 31
10 26 14 33
# can start by creating a function to read contents of
# each file and return the contents as a string
def readFile(file):
    contentsStr = ''
    for line in file:
        contentsStr += line
    return contentsStr

# Read all the header files header1, header2, header3
header1 = open('header1.txt','r')
header2 = open('header2.txt','r')
header3 = open('header3.txt','r')

# Read all the data files data1, data2, data3
data1 = open('data1.txt','r')
data2 = open('data2.txt','r')
data3 = open('data3.txt','r')

# Open/create output files asc1, asc2, asc3
asc1_outFile = open('asc1.txt','w')
asc2_outFile = open('asc2.txt','w')
asc3_outFile = open('asc3.txt','w')

# read contents of each header file and data file into string variabls
header1_contents = readFile(header1)
header2_contents = readFile(header2)
header3_contents = readFile(header3)

data1_contents = readFile(data1)
data2_contents = readFile(data2)
data3_contents = readFile(data3)

# Append the contents of each data file contents to its
# corresponding header file
asc1_contents = header1_contents + '\n' + data1_contents
asc2_contents = header2_contents + '\n' + data2_contents
asc3_contents = header3_contents + '\n' + data3_contents

# now write the necessary results to asc1.txt, asc2.txt, and
# asc3.txt output files respectively
asc1_outFile.write(asc1_contents)
asc2_outFile.write(asc2_contents)
asc3_outFile.write(asc3_contents)

# close the file streams
header1.close()
header2.close()
header3.close()
data1.close()
data2.close()
data3.close()
asc1_outFile.close()
asc2_outFile.close()
asc3_outFile.close()

# done!
#Glob is imported to get file names matching to the given pattern 

import glob
header=[]
data=[]

#Traversing through the file and getting the content

for files1 in glob.glob("directory/header*.txt"):
    a=open(files1,"r").read()
    header.append(a)

for files2 in glob.glob("directory/data*.txt"):
    a1=open(files2,"r").read()
    data.append(a1)

#Writng the content into the file

for i in range(1,len(data)+1):
    writer=open("directory/asc"+str(i)+".txt","w")
    writer.write(header[i-1]+"\n\n"+data[i-1])
    writer.close()
#Glob is imported to get file names matching to the given pattern 

import glob
header=[]
data=[]

#Traversing through the file and getting the content

for files1 in glob.glob("directory1/*.txt"):
    a=open(files1,"r").read()
    header.append(a)

for files2 in glob.glob("directory2/*.txt"):
    a1=open(files2,"r").read()
    data.append(a1)

#Writng the content into the file

for i in range(1,len(data)+1):
    writer=open("directory/asc"+str(i)+".txt","w")
    writer.write(header[i-1]+"\n\n"+data[i-1])
    writer.close()
for i in range(1,4):

    h = open('header{0}.txt'.format(i),'r')
    d = open('data{0}.txt'.format(i),'r')
    a = open('asc{0}.txt'.format(i),'a')

    hdata = h.readlines()
    ddata = d.readlines()
    a.writelines(hdata+ddata)
    a.close()