Home > Software design >  How to give a condition that if difference between two hexadecimal no is 2 then concatenate two rows
How to give a condition that if difference between two hexadecimal no is 2 then concatenate two rows

Time:10-20

I have a file which contain two columns, eg abc.txt

000000008b8c5200        afdgsdjhg143662367
000000008b8c5220        bcbiubcbueqihd1672
0000000056fb2620        adagsjhabnbcbh5369
0000000056fb2640        akbhjgbfre34168gff
0000000045ab4630        sgfjg36gdhw728g7dg
0000000045ab4650        jbshjdb7usvyysdu89

Here I need to concatenate 2nd row 2nd column with first row first column like this:

bcbiubcbueqihd1672afdgsdjhg143662367

Condition for concatenating:
only when (hexadecimal)difference between 2nd row, 1st column and 1st row, 1st column is 20. For this example it would be:

000000008b8c5220 - 000000008b8c5200 = 20.
0000000056fb2640 - 0000000056fb2620 = 20.
0000000045ab4650 - 0000000045ab4630 = 20.

Similarly for upcoming rows and columns. Write the results to a file with first row and concatenated data like this:

000000008b8c5200 bcbiubcbueqihd1672afdgsdjhg143662367
0000000056fb2620 akbhjgbfre34168gffadagsjhabnbcbh5369
0000000045ab4630 jbshjdb7usvyysdu89sgfjg36gdhw728g7dg

How can I do this?

CodePudding user response:

Here is how you could do it:

with open('input.txt', 'r') as f, open('output.txt', 'w') as g:
    out = []
    lines = [elem.strip().split() for elem in f] # lines will be list of lists, each list with 2 elements (-> 2 columns)
    for line_x, line_y in zip(lines[::2], lines[1::2]): # use zip and steps of 2 to iterate like: 1&2, 3&4, 5&6...
        num_x = int(line_x[0], base=16) # convert hex to int
        num_y = int(line_y[0], base=16)
        print(num_x, num_y, num_y - num_x)
        if num_y - num_x == 32:
            # if difference is 20, build string seperated by tab with the hex number and the concatenated data
            new_row = '\t'.join([line_x[0], line_y[1]   line_y[1]])
            out.append(new_row)
    g.write('\n'.join(out)) # write all new rows to output file

For the provided example it prints:

2341229056 2341229088 32
1459299872 1459299904 32
1168852528 1168852560 32

Because no row difference is 20 there will be no data in the output file.

CodePudding user response:

If I understand your question is how to make hex str to int (for the subtraction)

you can use the int command with 2nd parameter as the base (16)

>>> int('abc', 16)
2748
>>> int('000000008b8c5220',16) - int('000000008b8c5200',16) == 0x20
True

if your qustion is how to read the text from the file:

with open('filename.txt') as my_file:
    file_data = [line.replace('\n','').split('        ') for line in my_file if line]

and now you have a 2D array of strs of your data

CodePudding user response:

Everything in python is object. You can transform all values in string first File.txt converted into a string and use split or take some fragment of your text. let's see the example code:

string="SDF SDF"

a=string[3:5]
print(a)

After all strings you get, you have what you can organize and concatenate. The other way is you using split in text and organize all in the list using for to organize and concatenate. Let's see the example code:

stra = '20 40 60 80 69 70'
string=""
#split string by single space
chunks = stra.split(' ')
print(chunks)
for i in  range (len(chunks)):
    # put your conditions here and add at your string, follow this example or create your conditions here
    a=chunks[i]
    print(a)
    print(i)
    if i<(len(chunks)-1):
        b=chunks[i 1]
        subs = int(b)-int(a)
        if subs == 20:
            string  = f"{a} {b}"
  • Related