python - 根据Python中另一个文件中的元素对文本文件进行排序

标签 python

我正在尝试用 python 编写一段代码,根据另一个文件中元素的顺序对一个文本文件中的行进行排序。例如,我将“U4”作为第一个元素,因此我将在另一个文件中搜索 U4 并将包含此关键字的行作为第一行(排序)。除了 U1 等元素之外,以下代码运行良好,因为当我尝试搜索它时,它给了我 U1 、 U11 、 U111 、 U12 的结果,我应该做什么来修复它

代码:

 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:])

输出示例(请注意它适用于第一个输入,而不是第二个输入):

   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) );

第一个输入文件的示例(具有已排序元素的文件):

       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

最后是第二个输入文件的示例(我想要排序的文件)

         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) );

最佳答案

正如我在评论中所述,您可以使用 ==
查看您的示例输入,您的单词始终位于行中的第二个单词。因此,您可以从两个输入文件中拆分行,然后检查索引 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)

输出如下:

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) );

关于python - 根据Python中另一个文件中的元素对文本文件进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36154724/

相关文章:

python - 在 Python influxdb_client 的 Flux 查询中使用参数

python - 如何在不锁定 Vim 的情况下运行当前的 python 脚本?

python - 浏览所有 Bonjour 服务

python - 使用 print 和 for 循环函数打印 1 到 99

python - 无需在 Python 中导入即可查找模块的路径

python - 从文本 tkinter 传递值

javascript - 如何在python中将网页表数据转换为json对象或dict

python - 如何在python中读取文本文件的任意行?

python - Docker 上的 Odoo 开发

python - 使用 Numpy stride_tricks 获取非重叠数组 block