How can I sort lines and extract information in Python?
Question: I have this file text:

    
    
    << end of ENERGY.
    iupac_m_486_> OE1/2 will be swapped:     -136.1396        1     1
    openf___224_> Open           Dominio1.BL00100001.pdb
    wrpdb___568_> Residues, atoms, selected atoms:      268     2115     2115
    
    >> Summary of successfully produced loop models:
    Filename                          molpdf
    ----------------------------------------
    Dominio1.BL00010001.pdb         24.69530
    Dominio1.BL00020001.pdb         14.33748
    Dominio1.BL00030001.pdb         30.53454
    Dominio1.BL00040001.pdb         23.82516
    Dominio1.BL00050001.pdb         27.48684
    Dominio1.BL00060001.pdb         18.17364
    Dominio1.BL00070001.pdb         30.98407
    Dominio1.BL00080001.pdb         17.19927
    Dominio1.BL00090001.pdb         19.02460
    Dominio1.BL00100001.pdb         22.57086
    

I want sort the last 10 lines and find which file name is associated with the
smallest number, to use it in the second part of my script.

In the example file above, the file name I want is `Dominio1.BL00020001.pdb`,
because it's associated with the smallest number, but I want to find it
programmatically.

This code just show the lines that I need sort

    
    
    import itertools
    with open('%s'%W, "r") as text_file:
        for line in itertools.islice(text_file, 2210, 2220):
            print line
    CREATE CODE..(please Can somebody help me to do it?)
    

and find that the `Dominio1.BL00020001.pdb` has the smallest number and
continue reading and put it like "a", I can't use like this:

    
    
    model_initial= '%s'%a'
    


Answer: Something like this:

    
    
    import re
    def yield_items(filename):
      with open(filename) as f:
        for line in f:
          if re.match(r'\S+[.]pdb\s+(\d[\d.]+)(?:\s|\Z)', line):
            items = line.split()
            yield float(items[1]), items[0]
    print min(yield_items('input.txt'))
    

In contrast to the solutions of gboffi and Malik Brahimi, this solution uses
less memory: it uses memory only for 1 line at a time, so even if the file
contains a huge number of matching lines, this solution doesn't keep more than
1 of them in memory at the same time.

