2014-02-14 57 views
-1

我试图从文件中读取坐标并找到文件中每个两个相应原子之间的距离。 我想要在一列中计算出距离,在另一列中计算相应的原子名称。从文件中调用坐标的距离计算(Python2.7)

1 Br1 0 Br x,y,z 1.195 7.005 10.004 
2 Br2 0 Br x,y,z 1.432 5.040 6.816 
3 Br3 0 Br x,y,z -0.407 8.433 6.863 
4 Br4 0 Br x,y,z 3.344 8.375 7.107 
5 Fe1 0 Fe x,y,z 1.412 7.214 7.682 
6 Br5 0 Br x,y,z 2.813 12.506 12.949 
7 Br6 0 Br x,y,z 4.778 14.123 10.091 
8 Br7 0 Br x,y,z 6.563 12.765 13.175 
9 Br8 0 Br x,y,z 4.387 15.965 13.344 

例如:

Br1-Br2 1.5 

我遇到两个问题:

1-I不知道如何以命令程序来计算每个2对原子之间的欧几里德距离,我用split()从其他数据分割坐标,但我仍然不知道如何计算距离。我发现了一些关于same problem的信息,但是这些答案对我来说并不适用,因为它们是针对perl的。

atom_positions= open('Atoms.txt','r') 
revised_atom_positions = open('Atoms_atoms.txt','w') 
aline = atom_positions.readline()#for getting rid of the first line data which is general info 

for aline in atom_positions: 
    Values = aline.split() 
    dataline = values[1]+' , '+ values[5] + ' , ' + values[6] +' , '+values[7] 
    revised_atom_positions.write(dataline + '\n') 


atom_positions.close() 
revised_atom_positions.close() 

当我试图把它变成一个数组,并使用pdist我得到的错误:

TypeError: 'builtin_function_or_method' object has no attribute '__getitem__' 

2 - 我可以存储我的新数据与新秩序的其他文件,但我没有想法如何打电话给他们进行距离计算!有一些关于pdist的指导方针,但它对我也不起作用。

有没有解决这个问题的方法?

回答

1

您可以使用索引:

atom_positions= open('Atoms.txt','r') 
revised_atom_positions = open('Atoms_atoms.txt','w') 

lines = atom_positions.readlines() 

for i in range(0, len(lines)): 
    line = lines[i] 
    values = line.split() 
    dataline = values[1]+' , '+ values[5] + ' , ' + values[6] +' , '+values[7] 
    # You can access to next line with lines[i + 1] 
    revised_atom_positions.write(dataline + '\n') 


atom_positions.close() 
revised_atom_positions.close()