2012-08-04 66 views
0

我试图从每个文件中获取信息并使用它来创建一个新文件。每个文件都有一行由一系列数字组成。我希望一个文件的每一行与另一个文件的每一行对齐,然后将每个数字放在一个文件的一行中,以便与另一个文件中另一行的相同位置中的另一个数字相遇。文件'Volume.txt'的每行都移动了一个点(因此代码的k = j + 1)。多文件处理

* 操作完成后,我不断收到一个重复的汉字。那么我哪里错了?非常感谢! *

下面是代码:

e = open('C:/Users/MC/Desktop/EODP.txt', 'r') 
v = open('C:/Users/MC/Desktop/Z/Volume.txt', 'r') 
n = open('C:/Users/MC/Desktop/vovere.txt', 'w') 

m = 0 #Used later for letting me know that the first line in the new file completed 

for i in range(0, 3256): # there are 3257 lines per the two files EOPD and Volume 
    l = []    # create a list to put data in during operation 
    er = e.readline()  # 
    es = er.split(', ') # create a list the data from that line of file 
    vr = v.readline()  # 
    vs = vr.split(', ') # same 
    for j in range(len(es)): 
     k = j + 1   # file Volume is shifted one point ahead 
     try: 
      if float(vs[k]) == 0.0: 
       vovere = 0.0    # vovere is the name of the output for each point 
      elif vs[k] == '' or vs[k] == ' ': 
       vovere = 0.0 
      else: 
       vovere = float(es[j])/float(vs[k]) 
     except ValueError:   #kept getting this error: some points weren't numbers 
      vovere = 0.0  
     except IndexError:   # Each file didn't always exactly equal in line length 
      vovere = 0.0 
     la = l.append(float(vovere)) #Creates the list for each new line in new file 
    ls = str(la) 
    l1 = ls.replace('[', '') # Taking away extra notations so the new file is a little 
    l2 = l1.replace(']', '') # more clean and can be put into other programs 
    n.write(l2) 
    if m == 0:     # From here on out is just for me, not necessary** 
     print("The first line is done!") 
     m += 1 
    else: 
     pass 

e.close()      #** Except these of course 
print(e.closed) 
print("Yes, EOPD.txt is closed") 
v.close() 
print(v.closed) 
print("Yes, Volume.txt is closed") 
n.close() 
print(n.closed) 
print("Yes, vovere.txt is now ready for further manipulation!!") 
+0

这个问题不清楚,是每个文件中只有一行或多行在每个文件中?如果每个文件中有多行,每行上是否有多个数字?如果一行中有更多数字,分隔符是什么?我几乎跳过了阅读代码的过程,因为看起来写一些能够完成问题文本部分描述的工作的简短代码会更容易。 – LJNielsenDk 2012-08-04 16:48:45

+1

对此感到抱歉。我从中提取的两个文件每行都有多行。每行有多个数据点,每个数据点由逗号和空格分隔。我试图将第一行从一个文件匹配到另一个文件的第一行,然后从EODP.txt中获取数据点1,从Volume.txt获取数据点2,并在两行中​​指出每个其他数据,交错相对位置。我希望这对每一个后续行重复。每个文件具有相同的行数,但并不是必须使用每行所使用的相同数量的数据点。希望有帮助,谢谢! – mrfc 2012-08-04 17:00:59

+1

交错点含义:文件卷,第1行,第2点与文件EODP,第1行,第1点。然后文件卷第1行,第3点与文件EODP,第1行,第2点。重复整个文件EODP行长度1,应该有le len len(卷1)-1。 – mrfc 2012-08-04 17:08:37

回答

0

如果我明白问题的正确这应该做的伎俩:

from itertools import zip_longest # If this was python 2.x import izip_longest instead 

if __name__ == '__main__': 
    input1_lines = [] 
    input2_lines = [] 

    # Read all the input from first file 
    with open('./input1.txt', 'r') as input1: 
     input1_lines = input1.readlines() 

    # Read all the input from first file 
    with open('./input2.txt', 'r') as input2: 
     input2_lines = input2.readlines() 

    # Go through all the lines 
    output_lines = [] 
    for line_number_index in range(len(input1_lines)): 
    # For each line in each input file make a list of items and remove line breaks 
     line1_items = str(input1_lines[line_number_index]).replace('\n', '').split(', ') 
     line2_items = str(input2_lines[line_number_index]).replace('\n', '').split(', ')[1:] 

     # Go through the item lists and merge them, fill empty spots with fillvalue if there are more items in one of the lists 
     this_output_line = [] 
     for zip_entry in zip_longest(line1_items, line2_items, fillvalue='FILL_VALUE'): 
      this_output_line.append(', '.join(zip_entry)) 
     output_lines.append(', '.join(this_output_line)) 

    # Write out the file 
    with open('./output.txt', 'w') as output_file: 
     for line in output_lines: 
      output_file.write('%s\n' % line) 

更换FILL_VALUE,不管你要插入如果有更多的在合并它们时另一个行中的项目。

如果你需要的第一个项目是从input2.txt而不是input1.txt,开关line1_itemsline2_items各地for zip_entry in zip_longest(line1_items, line2_items, fillvalue='FILL_VALUE'):

输入2是一个地方的每一行的第一个项目将被忽略。

本我的测试数据:

input1.txt:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 
2, 22, 222, 2222 

input2.txt:

z, a, b, c, d, e, f, g, h, i, j, k 
z, a, aaa, aaaa 

结果:

0, a, 1, b, 2, c, 3, d, 4, e, 5, f, 6, g, 7, h, 8, i, 9, j, 10, k 
2, a, 22, aaa, 222, aaaa, 2222, FILL_VALUE