Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
input1.txt
info="0x101" Data="0x00000000"
info="0x1678a1" Data="0x0a56F001"
info="0x156A17" Data="0x0003F4a1"
info="0x18C550" Data="0x00000000"
info="0x145673" Data="000C60Fa2"

input2.txt
//PS name         above bit      below bit      original            1_info           2_info
//PS_AS_0         PS_00[31]      PS_00[00]      0x00000000          0x156A17[00]     0x156A17[31]  
//PS_PTO_A1       PS_01[31]      PS_01[00]      0x00000003          0x127A53[00]     0x127A53[31]
//PS_PFGD_C       PS_02[31]      PS_02[00]      0x00000000          0x170A23[00]     0x170A23[31]
//PS_RST_D2       PS_03[05]      PS_03[00]      0x00000003          0x1678A1[00]     0x1678A1[05] 
//PS_N_YD_C       PS_03[06]      PS_03[06]      0x00000000          0x1678A1[06]     0x1678A1[06]
//PS_1_FG         PS_03[31]      PS_03[07]      0x000000FF          0x1678A1[07]     0x1678A1[31]
//PS_F_23_ASD     PS_04[07]      PS_03[00]      0x00000000          0x18C550[00]     0x18C550[07]
//PS_A_0_STR      PS_04[15]      PS_04[08]      0x00000FFF          0x18C550[08]     0x18C550[15]
//PS_AD_0         PS_04[31]      PS_04[16]      0x00000000          0x18C550[16]     0x18C550[31]
//PS_P            PS_05[31]      PS_05[00]      0xFFFFFFFFFFFFFFFF  0x186cF0[00]     0x186cF0[31]


from this two i need to create another file in this format: out.txt
PS name         above bit      below bit      original            1_info           2_info            new         
PS_AS_0         PS_00[31]      PS_00[00]      0x00000000          0x156A17[00]     0x156A17[31]      0x0003F4a1 
PS_RST_D2       PS_03[05]      PS_03[00]      0x00000003          0x1678A1[00]     0x1678A1[05]      0x0a56F001
PS_N_YD_C       PS_03[06]      PS_03[06]      0x00000000          0x1678A1[06]     0x1678A1[06]      0x0a56F001
PS_1_FG         PS_03[31]      PS_03[07]      0x000000FF          0x1678A1[07]     0x1678A1[31]      0x0a56F001
PS_F_23_ASD     PS_04[07]      PS_03[00]      0x00000000          0x18C550[00]     0x18C550[07]      0x00000000
PS_A_0_STR      PS_04[15]      PS_04[08]      0x00000FFF          0x18C550[08]     0x18C550[15]      0x00000000
PS_AD_0         PS_04[31]      PS_04[16]      0x00000000          0x18C550[16]     0x18C550[31]      0x00000000

the output file is created in this manner:

read input2.txt and check 1_info value and if the same info value is there in input1.txt then write this line in input2.txt along with additional column new which will have value of data with respect to info of input1.txt.

eg: in input2.txt check 1_info value which is 0x156A17 and then check if this value is there in info of input1.txt. here input1.txt have this value 0x156A17 then write to out.txt along with data value of input1.txt 0x0003F4a1. like this:
PS_AS_0         PS_00[31]      PS_00[00]      0x00000000          0x156A17[00]     0x156A17[31]      0x0003F4a1

and if the 1_info value of input2.txt is not there in input1.txt then don't write that line to out.txt. for eg 0x127A53 this 1_info value of input2.txt is not there in info value of input1.txt. then don't write that line to out.txt.

What I have tried:

Python
file_1_content = None
file_2_content = None
with open("input.txt") as file_1:
    file_1_content = [line.strip() for line in file_1.readlines()]
with open("input2.txt") as file_2:
    file_2_content = [line.strip() for line in file_2.readlines()]

file_3_content = []

for line in file_1_content:
    if line not in file_2_content:
        file_3_content.append(line)

for line in file_2_content:
    if line not in file_1_content:
        file_3_content.append(line)

file_3_content = '\n'.join(file_3_content)
with open("out.txt", "w") as file_3:
    file_3.write(file_3_content)
print(f"Wrote file:\n{file_3_content}")


but this code just compare. it won't give output in expected format.
Posted
Updated 14-Nov-22 1:52am
v2

1 solution

It would probably be better if you use the values in input1.txt to create a Python dictionary[^], using the info field as the key, and the Data field as the value. It is then a simple matter to read input2.txt line by line using the text from the 1_info column to see if the key exists in the input1.txt file. If the key does not exist then do nothing. if the key does exist then append the value item to the line and write it to the output file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900