python - 如何枚举文件的特定列

标签 python python-2.7

我试图枚举输入文件的第二列,但我的代码给出了以下错误。如何修复我的代码以获得以下输出文件?

错误:

Traceback (most recent call last): File "./rename_atoms.py", line 7, in print(item+index) TypeError: cannot concatenate 'str' and 'int' objects

我的代码:

#!/usr/bin/python

with open ('input.gro', 'r') as f:
    for line in f:
        column=line.split()
        for index, item in enumerate(column[1]):
            print(item+index)

输入文件:

GRoups of Organic Molecules in ACtion for Science
   18
    1LIG      O    1   1.665   1.740   8.646
    1LIG      O    2   0.877   2.044   7.947
    1LIG      S    3   1.469   1.778   8.501
    1LIG      S    4   1.340   1.695   8.487
    1LIG      S    5   1.231   1.770   8.412
    1LIG      N    6   1.282   1.801   8.268
    1LIG      C    7   1.553   1.679   8.585
    1LIG      C    8   1.523   1.805   8.360
    1LIG      C    9   1.313   1.647   8.630
    1LIG      H   10   1.418   1.875   8.271
    1LIG      H   11   1.454   1.624   8.688
    1LIG      H   12   1.100   1.691   8.403
    1LIG      H   13   1.453   1.912   8.577
    1LIG      H   14   1.174   1.869   8.184
    1LIG      H   15   0.992   1.777   8.339
    1LIG      H   16   1.037   1.853   8.217
    1LIG      H   17   1.206   1.941   8.068
    1LIG      H   18   0.939   1.914   8.137
      0.00000   0.00000   0.00000

所需的输出文件:

GRoups of Organic Molecules in ACtion for Science
   18
    1LIG      O1    1   1.665   1.740   8.646
    1LIG      O2    2   0.877   2.044   7.947
    1LIG      S1    3   1.469   1.778   8.501
    1LIG      S2    4   1.340   1.695   8.487
    1LIG      S3    5   1.231   1.770   8.412
    1LIG      N1    6   1.282   1.801   8.268
    1LIG      C1    7   1.553   1.679   8.585
    1LIG      C2    8   1.523   1.805   8.360
    1LIG      C3    9   1.313   1.647   8.630
    1LIG      H1   10   1.418   1.875   8.271
    1LIG      H2   11   1.454   1.624   8.688
    1LIG      H3   12   1.100   1.691   8.403
    1LIG      H4   13   1.453   1.912   8.577
    1LIG      H5   14   1.174   1.869   8.184
    1LIG      H6   15   0.992   1.777   8.339
    1LIG      H7   16   1.037   1.853   8.217
    1LIG      H8   17   1.206   1.941   8.068
    1LIG      H9   18   0.939   1.914   8.137
      0.00000   0.00000   0.00000

最佳答案

问题出在这一行:

  for index, item in enumerate(column[1]):
>>      print(item+index)

您正在尝试添加一个 int (index) 和一个字符串 (item)。如果您只想将它​​们打印在一起,只需执行以下操作:

print(item, index)

请注意,默认情况下这会在参数之间添加空格。要删除它,请使用 sep 参数,并将其设置为空字符串。

但是,这可能不是您想要的,因为输出将类似于:

O 0
O 0
S 0

...等等。

所以,这样做:

for i, l in enumerate(f):
    column = l.split()
    print(column[1],i,sep='')

这将类似于:

O0
O1
S2

...等等。

快到了。为了获得所需的输出,我们需要维护每个元素的计数。所以,尝试这样的事情:

from collections import defaultdict

counts = defaultdict(int)
for l in f:
    column = l.split()
    counts[column[1]] += 1
    print(column[1], counts[column[1]], sep="")

这应该打印:

O1
O2
S1

...等等。

关于python - 如何枚举文件的特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40249440/

相关文章:

python - 如何在mongodb中以一个字段作为主键保存数据

python - socket,gethostbyaddr 的返回和写入文件如何拆分?

python - 从 JSON 文件加载数据

python - 对 Python 中 pandas 维度的一些规则进行分组

python - django命名空间NoReverseMatch

python - 使用 lxml 和 xpath 解析 xml 文件时如何保留 &

python - 使用seek(0)方法修改csv文件会创建不需要的重复输出

python - GitPython:如何使用 '--no-ff' pull 远程存储库?

python - Ngrok 无法完成隧道连接

python - 如果随机数悬停元素