pythonpython-3.xsplitpython-rereadlines

convert two input files into certain string rules by python


Let's say there are two input files as below:
input1.txt (only composed of hstep3_*)

hstep3_num00 = a5;
hstep3_num01 = 3b;
hstep3_num02 = 4f;
hstep3_num03 = 27;

input2.txt ( the letters inside brackets are some random characters split by ,)

some random strings that are not 'hstep' form
... 
match hstep1_num00 = {eau,t,nb,v,d}; // MATCH
match hstep1_num01 = {c,bul,kv,e}; // MATCH
... 
match hstep3_num00 = {u_ku,b,ntv,q}; // MATCH
match hstep3_num01 = {qq,rask,cb_p}; // MATCH
match hstep3_num02 = {c,a,ha,w,ykl}; // MATCH
match hstep3_num03 = {p,gu,enb_q_b,z,d}; // MATCH
...
some random strings that are not 'hstep' form

and What I want to do is to sort out all the left hand side of equation from input1.txt, and match the corresponding bracket and value from input2.txt.

So, the final output.txt looks as follows: output.txt

{u_ku,b,ntv,q}     = a5;
{qq,rask,cb_p}     = 3b;
{c,a,ha,w,ykl}     = 4f;
{p,gu,enb_q_b,z,d} = 27;

In order to do so by python, I've thought about readlines.split(). Also, since the number of Characters inside bracket are not always same by lines, I thought I have to use regular expressions to limit the range inside {} but It doesn't work as I expected...
Anybody give me any solutions or guidelines for this?

Any help would be much appreciated.. thanks!


Solution

  • The code below is not optimized, but it's so OP understands the processes involved a bit better

    # read input1 and turn into dict
    input1 = {}
    with open("input1.txt") as infile:
        for line in infile.readlines():
            key, value = line.split(" = ")
            input1[key] = value
    
    # read input 2 and store the maxlen value
    input2 = []
    maxlen = 0
    with open("input2.txt") as infile:
        for line in infile.readlines():
            # only process lines that start with "match hstep3"
            if line.startswith("match hstep3"):
                key = line.split(" ")[1]
                value = line.split("= ")[1].split(";")[0]
                input2.append([key, value])
                # get the maxlength and store it for future use
                maxlen = max(maxlen, len(value))
    
    # finally, produce the required output and write to file
    with open("output.txt", "w") as outfile:
        for line in input2:
            key, value = line
            # use an f-string to produce the required output
            newline = f"{value:<{maxlen}} = {input1[key]}"
            outfile.write(newline)
    

    output.txt file contents:

    {u_ku,b,ntv,q}     = a5;
    {qq,rask,cb_p}     = 3b;
    {c,a,ha,w,ykl}     = 4f;
    {p,gu,enb_q_b,z,d} = 27;