读取不同的文本文件并提取相同的索引行

时间:2015-09-25 11:17:11

标签: python

我不是python程序员。但我需要为软件制作输入文件。我有一个a.txt文件和b.txt,a.txt中的每一行都对应于b.txt中的“索引”。 A.TXT:

      0          0          0 L M L     41 ACC sh        1.008732   
      1          0          0 L * L     53 NCR sh        1.022706       
      2          1          1 L M L     18 ACC sh        1.025172    
      3          2          2 L M L     17 ACC sh        1.017734     
      4          2          2 L * L     21 NCR sh        1.025410  

b.txt:

#indexes:           0           0           0
       1        -0.375E+04         0.382E+01        
       2        -0.375E+04         0.432E+01        
       3        -0.376E+04         0.353E+01        
#indexes:           1           0           0
       1        -0.635E+04         0.331E+01
       2        -0.235E+04         0.238E+01
#indexes:           2           1           1
       1        -0.735E+04         0.093E+01
#indexes:           3           2           2
       1        -0.835E+04         0.331E+01
       2        -0.035E+04         0.438E+01  
#indexes:           4           2           2
       1        -0.475E+04         0.331E+01
       2        -0.365E+04         0.438E+01 

我需要在a.txt的第8列中使用“ACC”提取行,并将它们存储在新的a_new.txt中。

a_new.txt:

  0          0          0 L M L     41 ACC sh        1.008732   
  2          1          1 L M L     18 ACC sh        1.025172    
  3          2          2 L M L     17 ACC sh        1.017734   

然后读取b.txt文件,找到“索引”行,看看该行中的数字是否与ACC行相同(前3个coulmns),然后将该索引框存储在b_new.txt中:

b_new.txt:

#indexes:           0           0           0
   1        -0.375E+04         0.382E+01        
   2        -0.375E+04         0.432E+01        
   3        -0.376E+04         0.353E+01        
#indexes:           2           1           1
   1        -0.735E+04         0.093E+01
#indexes:           3           2           2
   1        -0.835E+04         0.331E+01
   2        -0.035E+04         0.438E+01  

如果有人能帮助我,我会很感激吗?

1 个答案:

答案 0 :(得分:0)

花了几分钟才能做到这一点:

import re
f = open('a.txt','r')
a = f.read()
f.close()
a_new = open('a_new.txt','w')
a_new.write('\n'.join(re.findall('(^.*ACC.*$)',a,re.M)))
a_new.close()
f = open('b.txt','r')
b = f.read()
f.close()
with open('b_new.txt','w') as b_new,open('a_new.txt','r') as a_new:
    inds = [x.replace(' ','') for x in re.findall('^\s*(\d\s*\d\s*\d)',a_new.read(),re.M)]
    for ind in inds:
        reg = '(#indexes:\s*{0}\s*{1}\s*{2}[\s\S]*?(?=#indexes|$))'.format(*list(ind))
        matches = re.findall(reg,b)
        b_new.write('\n'.join(matches))

运行后,a_new.txt将如下:

  0          0          0 L M L     41 ACC sh        1.008732
  2          1          1 L M L     18 ACC sh        1.025172
  3          2          2 L M L     17 ACC sh        1.017734

b_new.txt

#indexes:           0           0           0
       1        -0.375E+04         0.382E+01
       2        -0.375E+04         0.432E+01
       3        -0.376E+04         0.353E+01
#indexes:           2           1           1
       1        -0.735E+04         0.093E+01
#indexes:           3           2           2
       1        -0.835E+04         0.331E+01
       2        -0.035E+04         0.438E+01