Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python(numpy)读取混合格式的文本文件_Python_Text_Dictionary_Numpy_Readlines - Fatal编程技术网

Python(numpy)读取混合格式的文本文件

Python(numpy)读取混合格式的文本文件,python,text,dictionary,numpy,readlines,Python,Text,Dictionary,Numpy,Readlines,我有数千个这样的文件,我想为对应于原子['CG','CD1','CD2','CE1','CE2','CZ']的行提取列6,7,8的值 ATOM 1 CG TOLU 1 -0.437 -0.756 1.802 1.00 1.99 PRO0 ATOM 2 HG TOLU 1 -0.689 -1.123 2.786 1.00 0.00 PRO0 ATOM 3 CD1 TOLU 1

我有数千个这样的文件,我想为对应于原子['CG','CD1','CD2','CE1','CE2','CZ']的行提取列6,7,8的值

ATOM      1  CG  TOLU    1      -0.437  -0.756   1.802  1.00  1.99      PRO0
ATOM      2  HG  TOLU    1      -0.689  -1.123   2.786  1.00  0.00      PRO0
ATOM      3  CD1 TOLU    1       0.041  -1.623   0.811  1.00  1.99      PRO0
ATOM      4  HD1 TOLU    1       0.331  -2.603   1.162  1.00  0.00      PRO0
ATOM      5  CD2 TOLU    1      -0.692   0.547   1.352  1.00  1.99      PRO0
ATOM      6  HD2 TOLU    1      -1.131   1.264   2.030  1.00  0.00      PRO0
ATOM      7  CE1 TOLU    1       0.246  -1.276  -0.504  1.00  1.99      PRO0
ATOM      8  HE1 TOLU    1       0.596  -2.073  -1.144  1.00  0.00      PRO0
ATOM      9  CE2 TOLU    1      -0.331   0.991   0.063  1.00  1.99      PRO0
ATOM     10  HE2 TOLU    1      -0.565   2.030  -0.117  1.00  0.00      PRO0
ATOM     11  CZ  TOLU    1       0.136   0.076  -0.919  1.00  1.99      PRO0
ATOM     12  CT  TOLU    1       0.561   0.474  -2.282  1.00  0.00      PRO0
ATOM     13  H11 TOLU    1       0.529  -0.410  -2.955  1.00  0.00      PRO0
ATOM     14  H12 TOLU    1       1.574   0.930  -2.294  1.00  0.00      PRO0
ATOM     15  H13 TOLU    1      -0.203   1.165  -2.699  1.00  0.00      PRO0
ATOM     16  CG  TOLU    2       5.140   1.762  -1.390  1.00  1.99      PRO0
ATOM     17  HG  TOLU    2       5.815   1.717  -2.231  1.00  0.00      PRO0
ATOM     18  CD1 TOLU    2       4.578   0.647  -0.862  1.00  1.99      PRO0
ATOM     19  HD1 TOLU    2       4.835  -0.329  -1.246  1.00  0.00      PRO0
ATOM     20  CD2 TOLU    2       4.786   3.044  -0.824  1.00  1.99      PRO0
ATOM     21  HD2 TOLU    2       5.184   3.982  -1.181  1.00  0.00      PRO0
ATOM     22  CE1 TOLU    2       3.734   0.667   0.248  1.00  1.99      PRO0
ATOM     23  HE1 TOLU    2       3.131  -0.167   0.574  1.00  0.00      PRO0
ATOM     24  CE2 TOLU    2       4.042   3.068   0.321  1.00  1.99      PRO0
ATOM     25  HE2 TOLU    2       3.753   3.969   0.841  1.00  0.00      PRO0
ATOM     26  CZ  TOLU    2       3.465   1.886   0.893  1.00  1.99      PRO0
ATOM     27  CT  TOLU    2       2.501   1.806   2.157  1.00  0.00      PRO0
ATOM     28  H11 TOLU    2       2.361   0.712   2.283  1.00  0.00      PRO0
ATOM     29  H12 TOLU    2       1.490   2.181   1.890  1.00  0.00      PRO0
ATOM     30  H13 TOLU    2       2.845   2.513   2.943  1.00  0.00      PRO0
TER
END
注意,上面提到的每个原子都有两行。因此,我认为两本有12个键的字典最适合我的目标,就像这样

{1: {'CG':(0,0,0), 'CD1':(0,0,0), 'CD2':(0,0,0), 'CE1':(0,0,0), 'CE2':(0,0,0), 'CZ':(0,0,0)},
2: {'CG':(0,0,0), 'CD1':(0,0,0), 'CD2':(0,0,0), 'CE1':(0,0,0), 'CE2':(0,0,0), 'CZ':(0,0,0)}}
其中,第一个键(1,2)指的是第五列


您能告诉我一种读取文件并将每个值元组分配到字典中正确位置的可靠方法吗?我可以使用多个if条件,但我认为一定有更好的方法(可能是使用numpy)

解析文件的一种方法是
numpy.genfromtxt()

在此之后,
数据将被删除

[('CG', 1, -0.437, -0.756, 1.802),
 ('CD1', 1, 0.041, -1.623, 0.811),
 ('CD2', 1, -0.692, 0.547, 1.352),
 ('CE1', 1, 0.246, -1.276, -0.504),
 ('CE2', 1, -0.331, 0.991, 0.063),
 ('CZ', 1, 0.136, 0.076, -0.919),
 ('CG', 2, 5.14, 1.762, -1.39),
 ('CD1', 2, 4.578, 0.647, -0.862),
 ('CD2', 2, 4.786, 3.044, -0.824),
 ('CE1', 2, 3.734, 0.667, 0.248),
 ('CE2', 2, 4.042, 3.068, 0.321),
 ('CZ', 2, 3.465, 1.886, 0.893)]
现在可以很容易地将数据以您需要的任何字典格式保存。

这将完成以下工作:

atmlist = ['CG', 'CD1', 'CD2', 'CE1', 'CE2', 'CZ']
def Read_PDB(filename):
   coord={r:{k:(0,0,0) for k in atmlist} for r in [0,1]}
   try:
      f = open(filename, 'r')
   except IOError as err:
      print ("I/O error({0}): {1}".format(err.errno, err.strerror))
      quit()

   for line in f:
      for at in atmlist:
         if (line.find(at) == 13):
            line = line.strip()
            temp = line.split()
            crd = (float(temp[5]), float(temp[6]), float(temp[7]))
            coord[int(temp[4])-1][at] = crd;

   return coord` 

可能会有帮助->这很有效,只是我应该更改输出字典的索引!谢谢,我不认为捕获异常只是为了稍后以不同的方式抛出它有什么意义,但这只是一个小细节。
atmlist = ['CG', 'CD1', 'CD2', 'CE1', 'CE2', 'CZ']
def Read_PDB(filename):
   coord={r:{k:(0,0,0) for k in atmlist} for r in [0,1]}
   try:
      f = open(filename, 'r')
   except IOError as err:
      print ("I/O error({0}): {1}".format(err.errno, err.strerror))
      quit()

   for line in f:
      for at in atmlist:
         if (line.find(at) == 13):
            line = line.strip()
            temp = line.split()
            crd = (float(temp[5]), float(temp[6]), float(temp[7]))
            coord[int(temp[4])-1][at] = crd;

   return coord`