首页 > 解决方案 > 匹配时从第一个文件中查找数据并在file2中匹配行之后的下一行打印

问题描述

file1

1 AN3D3BWP210H6P51CNODSVT-(.A1(Y1),.A2(),.A3(),.Z(v1)); |2
2 BUFFSKFD32BWP210H6P51CNODSVT-(.I(v1),.Z(v2)); |3
3 GND2D1BWP210H6P51CNODSVT-(.A1(v2),.A2(),.ZN(v3)); |5
4 CKBD6BWP210H6P51CNODSVT-(.I(v3),.Z(v4)); |6
5 BUFFSKRD32BWP210H6P51CNODSVT-(.I(v4),.Z(v5)); |7
6 OIAI21D2BWP210H6P51CNODSVT-(.A1(v5),.A2(),.B(),.ZN(v6)); |10
7 AOI33D1BWP210H6P51CNODSVT-(.A1(v6),.A2(),.A3(),.B1(),.B2(),.B3(),.ZN(v7)); |14
8 ND2D4BWP210H6P51CNODSVT-(.A1(v7),.A2(),.ZN(v8)); |16

file2作为

9 AO21D4BWP210H6P51CNODSVT-(.A1(Y2),.A2(),.B(),.Z(v9)); |2
10 AOI221D4BWP210H6P51CNODSVT-(.A1(v9),.A2(),.B1(),.B2(),.C(),.ZN(v10)); |6
11 AOI21OPTPAD4BWP210H6P51CNODSVT-(.A1(v10),.A2(),.B(),.ZN(v11)); |9
12 AN3D6BWP210H6P51CNODSVT-(.A1(v11),.A2(),.A3(),.Z(v12)); |11
13 IND4D2BWP210H6P51CNODSVT-(.A1(v12),.B1(),.B2(),.B3(),.ZN(v13)); |16

我想compare last digit i.e digit after | in both the files并且如果它们匹配,我想从file1中获取该匹配行的最后一个括号数据,并将其复制到()file2中匹配行之后的下一行并写入file2。

在上面的输入文件中,line1 from file1 matches with line 9 of file2所以我想复制最后一个括号数据(v1) from file1,即替换()file2 中匹配行之后的下一行,即as line 9 was matched in file2 then replace its next line i.e line 10 () with last line bracket data of file1 and append in file2

我尝试了下面的代码,它工作正常,但替换()发生在 file2 的同一匹配行中,而不是在 file2 的下一行匹配行中

with open('file1','r') as fin:
   for line in fin:
       l=line[-5:]
       l2=re.sub('^.*\((.*?)\)[^\(]*$','\g<1>',line)   ### to find the value of last bracket 

fout=open('file2','r')
fout1=open('file2','a')

for line2 in fout:                                                    
   if "()" in line2:                                                           
      k=line2[-5:]                                                        
      if l==k:                                                                    
          k=line2.replace("()","("+l2+")",1)                                                               
          print >> fout1,k+'\n'

我想要输出如下

9 AO21D4BWP210H6P51CNODSVT-(.A1(Y2),.A2(),.B(),.Z(v9)); |2
10 AOI221D4BWP210H6P51CNODSVT-(.A1(v9),.A2(),.B1(),.B2(),.C(),.ZN(v10)); |6
11 AOI21OPTPAD4BWP210H6P51CNODSVT-(.A1(v10),.A2(),.B(),.ZN(v11)); |9
12 AN3D6BWP210H6P51CNODSVT-(.A1(v11),.A2(),.A3(),.Z(v12)); |11
13 IND4D2BWP210H6P51CNODSVT-(.A1(v12),.B1(),.B2(),.B3(),.ZN(v13)); |16

10 AOI221D4BWP210H6P51CNODSVT-(.A1(v9),.A2(v1),.B1(),.B2(),.C(),.ZN(v10)); |6
11 AOI21OPTPAD4BWP210H6P51CNODSVT-(.A1(v10),.A2(v4),.B(),.ZN(v11)); |9

标签: python

解决方案


这是一个使用字符串列表来解决临时文件问题的解决方案。只要替换行的数量不会太大而无法保存在内存中,这将正常工作。

它还假设您不会遇到来自 file2 的两个相邻行与 file1 的同一行匹配的情况,因为这next(fout)意味着不会针对 file1 检查下一行。

import re

new_lines = []
with open('file1','r') as fin:
   for line in fin:
      l=line[-5:]
      l2=re.sub('^.*\((.*?)\)[^\(]*$','\g<1>',line)   ### to find the value of last bracket

      fout=open('file2','r')

      # Store altered lines in a list of strings
      for line2 in fout:
         k=line2[-5:]
         if "()" in line2 and l==k:
            try:
               nextline = next(fout)
               k=nextline.replace("()","("+l2+")",1)
               new_lines.append(k)
            except StopIteration: # Don't care if we reach end of file2
               pass

fout.close()

# Now write list of strings to file
with open('file2', 'a') as fout:
   fout.write("\n")
   for line in new_lines:
      fout.write(line)


推荐阅读