如何在python中对文本文件中的行进行排序并选择它的某些字符?

时间:2015-01-28 19:26:16

标签: python

我的文本文件的行是:

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

我想创建一个代码,用于选择编号最小的行号(最后10行)(标识),并读取.pdb的名称(只有编号最小的行的24个字符) 。因为,我需要确定具有最小编号的.pdb是什么,并在其他脚本中像字符串一样使用它,如下所示:

  

模型=&#39;%S&#39;%R

其中&#39;%s&#39;%R是我需要的.pdb名称

我该怎么做?

4 个答案:

答案 0 :(得分:1)

您需要使用min函数和正确的密钥:

>>> min(s.split('\n\n'),key=lambda x:float(x.split()[-1])).split()[0]
'Dominio1.BL00020001.pdb'

演示:

>>> s="""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"""

>>> min(s.split('\n\n'),key=lambda x:float(x.split()[-1]))
'Dominio1.BL00020001.pdb 14.33748'
>>> min(s.split('\n\n'),key=lambda x:float(x.split()[-1])).split()[0]
'Dominio1.BL00020001.pdb'

答案 1 :(得分:0)

正常的文件读取操作将执行

data = file.readlines()
pdb_files = []
float_values = []
for line in data:
    pdb,float_value = line.split()
    pdb_files.append(pdb)
    float_values.append(float(float_value))
min_float_index = float_values.indexof(min(float_values))
print pdb_files.index(min_float_index)

此代码将数据存储在两个列表中,并查找给定的浮点值中的最小值。然后打印pdb文件名的相应条目

答案 2 :(得分:0)

试试这个:

def get_minimal_value_entry(file_name):
    with open(file_name, 'r') as f:

        # the value of a  line is the second member of 'split' result
        key = lambda x: float(x.strip().split()[1])

        return min(f, key=key).split()[0]

# 'test' file holds the data...
print get_minimal_value_entry('test')

# prints Dominio1.BL00020001.pdb

如果您有空行,请使用itertools.ifilter过滤空行:

from itertools import ifilter

def get_minimal_value_entry(file_name):
    with open(file_name, 'r') as f:

        # the value of a  line is the second member of 'split' result
        key = lambda x: float(x.strip().split()[1])

        return min(ifilter(lambda x: x.split(), f), key=key).split()[0]

# 'test' file holds the data...
print get_minimal_value_entry('test')

# prints Dominio1.BL00020001.pdb

答案 3 :(得分:0)

我使用Python re

file.txt

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

sorts.py

import re

lines = open('file.txt').readlines() # readlines
lines = [i.strip() for i in lines] # remove newlines

lines = [re.sub('\s+', ' ', i) for i in lines] # remove extra spaces
lines = [i.split(' ') for i in lines] # split by space

lines = [i for i in lines if i != ['']] # remove empty lines
lines = sorted(lines, key = lambda i: float(i[1])) # sort by id

print lines[0][0] # print item with least id