python - 匹配时从第一个文件中查找数据并在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
解决方案
这是一个使用字符串列表来解决临时文件问题的解决方案。只要替换行的数量不会太大而无法保存在内存中,这将正常工作。
它还假设您不会遇到来自 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)
推荐阅读
- html - 我该如何解决?我的引导模式弹出到屏幕的最右侧而不是在中心?
- python - Python:防止列表中的连续重复
- ios - 如何仅在 swift 中禁用标签栏标签?
- c# - 将数组添加到 POST 参数 C#
- react-native - 如何为应用程序 React Native 添加提醒?
- microsoft-graph-api - 从图形 api 创建一个 SPO 经典新闻页面
- python - 使用相同密钥/IV 的 Python AES CTR 模式
- divi - 无法将自定义 javascript 添加到 divi
- reactjs - Redux - 我应该从状态或 api 调用中获取产品详细信息吗?
- r - 如何计算 R 中的列总和,然后使用 data.table 库绘制它