简体   繁体   English

在2个文件中查找公共行,从file1写入公共行,从文件2写入非公共行

[英]Find common lines in 2 files, write common line from file1 and non common line from file 2

I am trying to find common lines in file 1 and file 2. If the common line exists, i want to write the line from file 2, otherwise print the non common line from file 1. fin1 and fin2 are the file handles here. 我试图找到文件1和文件2中的公共行。如果公共行存在,我想从文件2写入行,否则从文件1打印非公共行.fin1和fin2是这里的文件句柄。 Its reading the lines fine and there are common lines but i am not getting anything at all in teh output file. 它读取线条很好,有共同的线条,但我没有在输出文件中得到任何东西。

flag=0
list1=fin1.readlines()
list2=fin2.readlines()
for i in list1:
    for j in list2:
        if i.strip() in j.strip():
            frealout.write(j)
            flag=1
            break
    if flag==0:
        frealout.write(j)
    flag=0

Here is the input file structure file1 这是输入文件结构file1

ckgridu1n0
top_vli_z399a

here is the input file structure 2 这是输入文件结构2

   input node          ckgridu1n0,  
         input node [195:0]  top_vli_z399a, 

if flag==0: frealout.write(j) if flag == 0:frealout.write(j)

you write j value out of it's visibility scope. 你从它的可见范围中写出j值。 It really is not initialized at that point You should change j to i 它真的没有初始化你应该将j改为i

if flag==0: frealout.write(i) if flag == 0:frealout.write(i)

According to 根据

otherwise print the non common line from file 1 否则从文件1打印非公共线

I think you want to do 我想你想做

if flag==0:
    frealout.write(i)

instead of 代替

if flag==0:
    frealout.write(j)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM