如何使用python将头文件与数据文件组合?

时间:2015-06-11 12:02:24

标签: python

我有分隔文件,一部分文件只包含标题信息,如下例所示: 〜" header1.txt"〜

的内容
a 3
b 2
c 4

〜" header2.txt"〜

的内容
a 4
b 3
c 5

〜" header3.txt"〜

的内容
a 1
b 7
c 6

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

〜" data1.txt"〜

的内容
10 20 30 40
20 14 22 33

〜" data2.txt"〜

的内容
11 21 31 41
21 24 12 23

〜" data3.txt"〜

的内容
21 22 11 31
10 26 14 33

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

〜" asc1.txt"〜

的内容
a 3
b 2
c 4

10 20 30 40
20 14 22 33

〜" asc2.txt"〜

的内容
a 4
b 3
c 5

11 21 31 41
21 24 12 23

〜" asc3.txt"〜

的内容
a 1
b 7
c 6

21 22 11 31
10 26 14 33

任何人都可以在python中给我一些帮助吗?谢谢!

3 个答案:

答案 0 :(得分:1)

试试这个(用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源文件相同的目录中创建。

答案 1 :(得分:1)

如果你真的想在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,并且都遵循您使用的相同命名约定。

答案 2 :(得分:0)

如果头文件的数量等于数据文件的数量

,则此方法有效
#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()