Never too late
Never too late

Reputation: 83

Sorting text files based on elements from another file in python

I am trying to write a code in python that sort the lines in one text file based on the order of elements in another file. For example I have "U4" as the first element so I'll search for U4 in the other file and put the line that contain this key word as first line (sorting). The following code worked perfectly except for elements such as U1 , because when I tried to search for that it gave me the results for U1 , U11 , U111 , U12 , what should I do to fix it

code :

 def main(argv):


  inputs = []

  #Get python script arguments: Input/Output files

   inputfilea = argv[0]
   inputfileb = argv[1]
   outputfile = argv[2]

   print 'Input filea is:\t', inputfilea
   print 'Input fileb is:\t', inputfileb
   print 'Output file is:\t', outputfile
   f_out = open(outputfile, 'w');

  with open(inputfilea,"r") as infile:
        for line in infile:
            if line.startswith(" "):            
                    partial_in= line.replace('cell', "")
                    partial_in= partial_in.replace(' ', "")
                    partial_in= partial_in.strip('\n')

                    with open(inputfileb,"r") as infileb:
                           for line in infileb:
                            if partial_in in line:

                                print line


   #call main

   if __name__ == "__main__":

     main(sys.argv[1:])

Sample of the output ( see that it works for first input and not the second) :

   the input from the first file is DFF_0/Q_reg
   the corresponding line from the second file is 
    DFFSR \DFF_0/Q_reg  ( .D(G10), .CLK(CK), .R(R), .Q(G5) );

    the input from the first file is U1

    the corresponding line from the second file is 
     OR2X1 U12 ( .A(G0), .B(n4), .Y(n9) );

     the corresponding line from the second file is 
      AND2X1 U11 ( .A(n3), .B(n9), .Y(n6) );

     the corresponding line from the second file is 
     INVX1 U15 ( .A(G5), .Y(n13) );

     the corresponding line from the second file is 
     INVX1 U16 ( .A(n13), .Y(n14) );

     the corresponding line from the second file is 
     INVX1 U13 ( .A(G7), .Y(n11) );

    the corresponding line from the second file is 
    INVX1 U14 ( .A(n11), .Y(n12) );

    the corresponding line from the second file is 
     INVX1 U1 ( .A(G17), .Y(n1) );

Sample of the first input file ( the one that have the sorted elements):

       module s27
        cell U4
        cell U12
        cell U3
        cell U11
        cell U15
        cell U16
        cell U13
        cell U14
        cell U10
        cell U9
        cell U8
        cell U7
        cell U6
        cell DFF_0/Q_reg
        cell U1
        cell DFF_1/Q_reg
        cell U2

And finally sample of the second input file( the one I want to sort)

         INVX1 U4 ( .A(G6), .Y(n4) );
         OR2X1 U12 ( .A(G0), .B(n4), .Y(n9) );
         INVX1 U3 ( .A(G3), .Y(n3) );
         AND2X1 U11 ( .A(n3), .B(n9), .Y(n6) );
         INVX1 U15 ( .A(G5), .Y(n13) );
         INVX1 U16 ( .A(n13), .Y(n14) );
         INVX1 U13 ( .A(G7), .Y(n11) );
         INVX1 U14 ( .A(n11), .Y(n12) );
         OR2X1 U10 ( .A(G1), .B(n12), .Y(n5) );
         AND2X1 U9 ( .A(n5), .B(n9), .Y(n8) );
         OR2X1 U8 ( .A(n14), .B(n8), .Y(n7) );
         OR2X1 U7 ( .A(n6), .B(n7), .Y(G17) );
         AND2X1 U6 ( .A(G0), .B(G17), .Y(G10) );
         DFFSR \DFF_0/Q_reg  ( .D(G10), .CLK(CK), .R(R), .Q(G5) );
         INVX1 U1 ( .A(G17), .Y(n1) );
         DFFSR \DFF_1/Q_reg  ( .D(n1), .CLK(CK), .R(R), .Q(G6) );
         INVX1 U2 ( .A(G2), .Y(n2) );
         AND2X1 U5 ( .A(n5), .B(n2), .Y(G13) );
         DFFSR \DFF_2/Q_reg  ( .D(G13), .CLK(CK), .R(R), .Q(G7) );

Upvotes: 1

Views: 442

Answers (1)

Lafexlos
Lafexlos

Reputation: 7735

As I stated in comments, you can use ==.
Looking at your sample inputs, your words are always at second word in your lines. So you can split the lines from both input files then check their elements at index-1.

with open(inputfilea,"r") as infile:
    for line in infile:
        if line.startswith(" "):
            partial_in = line.strip().split()[1] #splits the line and gets 2nd word
            with open(inputfileb,"r") as infileb:
                for line2 in infileb:
                    if line2: #check if it's not empty line
                    #last strip added because of DFF lines has backslashes infront of them
                        if partial_in == line2.strip().split()[1].strip("\\"):
                            print partial_in, line2,
                            #since you wanted to write to a file, instead of print
                            #f_out.write(line2)

Which outputs this:

U4          INVX1 U4 ( .A(G6), .Y(n4) );
U12          OR2X1 U12 ( .A(G0), .B(n4), .Y(n9) );
U3          INVX1 U3 ( .A(G3), .Y(n3) );
U11          AND2X1 U11 ( .A(n3), .B(n9), .Y(n6) );
U15          INVX1 U15 ( .A(G5), .Y(n13) );
U16          INVX1 U16 ( .A(n13), .Y(n14) );
U13          INVX1 U13 ( .A(G7), .Y(n11) );
U14          INVX1 U14 ( .A(n11), .Y(n12) );
U10          OR2X1 U10 ( .A(G1), .B(n12), .Y(n5) );
U9          AND2X1 U9 ( .A(n5), .B(n9), .Y(n8) );
U8          OR2X1 U8 ( .A(n14), .B(n8), .Y(n7) );
U7          OR2X1 U7 ( .A(n6), .B(n7), .Y(G17) );
U6          AND2X1 U6 ( .A(G0), .B(G17), .Y(G10) );
DFF_0/Q_reg          DFFSR \DFF_0/Q_reg  ( .D(G10), .CLK(CK), .R(R), .Q(G5) );
U1          INVX1 U1 ( .A(G17), .Y(n1) );
DFF_1/Q_reg          DFFSR \DFF_1/Q_reg  ( .D(n1), .CLK(CK), .R(R), .Q(G6) );
U2          INVX1 U2 ( .A(G2), .Y(n2) );

Upvotes: 1

Related Questions