【发布时间】:2019-09-26 15:21:17
【问题描述】:
我正在编写代码来组合文本文件,但遇到了一个我无法解决的错误。谷歌帮不上忙。
代码是:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 26 09:51:55 2019
@author: comp
"""
import numpy as np
NAME = input("Enter Molecule ID: ")
NAME_IN = NAME+'_apo-1acl.RMSD'
DATA = []
DATA = np.genfromtxt(NAME_IN, skip_header=7, dtype=None, delimiter=' ')
文本文件是:
RMSD TABLE
__________
_____________________________________________________________________
| | | | | |
Rank | Sub- | Run | Binding | Cluster | Reference | Grep
| Rank | | Energy | RMSD | RMSD | Pattern
_____|______|______|___________|_________|_________________|___________
1 1 8 -7.23 0.00 93.07 RANKING
1 2 9 -6.79 1.39 92.64 RANKING
2 1 16 -7.18 0.00 93.19 RANKING
3 1 2 -6.93 0.00 93.38 RANKING
3 2 17 -6.84 0.23 93.45 RANKING
4 1 15 -6.55 0.00 91.83 RANKING
4 2 7 -6.34 0.33 91.77 RANKING
5 1 5 -6.41 0.00 93.05 RANKING
6 1 3 -6.36 0.00 92.84 RANKING
6 2 10 -6.28 0.47 92.92 RANKING
6 3 6 -6.27 0.43 92.82 RANKING
6 4 18 -6.25 0.32 92.88 RANKING
6 5 13 -6.24 0.96 92.75 RANKING
6 6 1 -6.24 0.87 92.60 RANKING
6 7 14 -6.21 0.51 92.90 RANKING
6 8 11 -6.14 0.98 92.78 RANKING
6 9 20 -6.11 0.71 92.67 RANKING
6 10 19 -6.01 1.36 93.00 RANKING
7 1 12 -6.30 0.00 93.28 RANKING
8 1 4 -5.85 0.00 92.97 RANKING
_______________________________________________________________________
错误是:
Traceback (most recent call last):
File "/home/comp/Apps/Models/1-PhosphorusLigands/CombinedLigands/MOL/Docking/Results/RMSDTable/CombineRMSDFiles.py", line 14, in <module>
DATA = np.genfromtxt(NAME_IN, skip_header=7, dtype=None, delimiter=' ')
File "/home/comp/Apps/Miniconda3/lib/python3.7/site-packages/numpy/lib/npyio.py", line 2075, in genfromtxt
raise ValueError(errmsg)
ValueError: Some errors were detected !
Line #9 (got 42 columns instead of 1)
Line #10 (got 42 columns instead of 1)
Line #11 (got 41 columns instead of 1)
Line #12 (got 42 columns instead of 1)
Line #13 (got 41 columns instead of 1)
Line #14 (got 41 columns instead of 1)
Line #15 (got 42 columns instead of 1)
Line #16 (got 42 columns instead of 1)
Line #17 (got 42 columns instead of 1)
Line #18 (got 41 columns instead of 1)
Line #19 (got 42 columns instead of 1)
Line #20 (got 41 columns instead of 1)
Line #21 (got 41 columns instead of 1)
Line #22 (got 42 columns instead of 1)
Line #23 (got 41 columns instead of 1)
Line #24 (got 41 columns instead of 1)
Line #25 (got 41 columns instead of 1)
Line #26 (got 40 columns instead of 1)
Line #27 (got 41 columns instead of 1)
Line #28 (got 42 columns instead of 1)
此时,我什至不知道如何提问。
【问题讨论】:
-
如果样本数据的对齐方式与数据文件中的对齐方式完全相同,很难确定。显然 numpy 预计每行只有一列,而分隔符
' '表示 42。也许您可以 1)重新格式化您发布的示例数据(首选),2)将文件内容发布到 pastebin 3)将文件上传到某处并共享链接 -
首先我建议删除
delimiter参数;默认值为“空白”,比您提供的更通用。 -
显然跳过后的第一行没有被提供的分隔符分割。所以
genfromtxt期望所有行都具有相同的列数 - 1。但后续行有 41 或 42 列(由分隔符定义)。genfromtxt期望所有行具有相同的列数。更仔细地检查文件。 -
如果我算对了,您应该跳过 8 个标题行。如果你得到正确的skip_header和delimiter,你应该得到一个包含7个字段、3个整数、3个浮点数和一个字符串的一维结构化数组。
标签: numpy genfromtxt