从文本中删除重复行

2024-04-19 07:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从txt文件中删除重复的行,即:

ATOM      1  N   MET B   1      43.567   2.228  13.359  1.00159.33           N  
ATOM      2  N   MET B   1      43.391   2.228  74.594  1.00159.33           N  
ATOM      3  CA  MET B   1      42.581   2.361  14.428  1.00160.56           C  
ATOM      4  CA  MET B   1      44.377   2.361  73.525  1.00160.56           C 

所以我想删除行:

ATOM      2  N   MET B   1      43.391   2.228  74.594  1.00159.33           N  
ATOM      4  CA  MET B   1      44.377   2.361  73.525  1.00160.56           C 

我试着用这段代码来实现这一点,但不幸的是,它不起作用。你知道吗

f=open("A.pdb").readlines()
lis=[]
for line in f:
    lis.append(line)
print (lis) 
length=len(lis)
element=0
array=[]
while element<length:
    if lis[element][13:16] == lis[element+1][13:16]:
        array.append(element)


for elements in array:
    lis.pop(array[elements])

Tags: 文件代码intxtforlineelementselement
1条回答
网友
1楼 · 发布于 2024-04-19 07:27:06

此版本将“N N CA N”更改为“N CA N”,这是您的要求吗?你知道吗

result = []
previous_keyword = None
with open('A.pdb') as f:
    for line in f:
        # use these five lines if keyword is fixed at 3rd column, and columns are separated by whitespace
        try:
            keyword = line.split()[2]
        except:
            print('Line with unknown format: ' + line)
            continue

        # use this one if the keyword is fixed at position[13:16]
        #keyword = line[13:16]

        if keyword != previous_keyword:
            result.append(line)
            #result.append(line.rstrip())     use this one if you don't want trailing 'newline'
            previous_keyword = keyword

for x in result:
    print x

你的程序“暂停并且永远不会完成”的原因是:在这个迭代中,你永远不会增加'element'

while element<length:
    if lis[element][13:16] == lis[element+1][13:16]:
        array.append(element)

相关问题 更多 >