The two input files are given below,
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: Expected output
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
read 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.
This is current attempt: but it will only generate comparison
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)
how can i get the out.txt
in Expected output format.
CodePudding user response:
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.
Just follow this.
- Before reading
input2.txt
, you need to construct a set for quick indexing, and a dictionary for obtaining data.
file1_info_set = set()
file1_info_data = {} # info -> data
with open("input1.txt") as file:
for line in file: # readlines() is unnecessary
# you can also use regular expression, but it seems unnecessary
info, data = (e for i, e in enumerate(line.split('"')) if i%2)
file1_info_set.add(info.lower())
file1_info_data[info.lower()] = data
The set and dictionary you get are,
print(file1_info_set)
# {'0x101', '0x145673', '0x156a17', '0x1678a1', '0x18c550'}
print(file1_info_data)
# {'0x101': '0x00000000', '0x1678a1': '0x0a56F001', '0x156a17': '0x0003F4a1', '0x18c550': '0x00000000', '0x145673': '000C60Fa2'}
- Read each line from
input2.txt
and decide if it should appear in the ouput file. You can write the output file during this process.
with open("input2.txt") as file, open("output.txt", "w") as outfile:
is_title = True
for line in file:
line = line.rstrip().lstrip("/")
if is_title:
outfile.write(line f" new\n")
is_title = False
else:
info = line.split()[4].split('[')[0].lower() # also, you can use regular expression here
if info in file1_info_set:
outfile.write(line f" {file1_info_data.get(info)}\n")
You can adjust the concrete string format by yourself.