qasim qasim - 1 month ago 9
Python Question

how to enumerate a specific column of a file

I was trying to enumerate the second column of the input file but my code gives the below error. How can I fix my code to get the below output file?

Error:


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


My code:

#!/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)


Input file:

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


Desired output file:

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

Answer

The problem is in this line:

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

You are trying to add an int (index) and a string (item). If you just want them printed together, simply do:

print(item, index)

Note that this will add a space between the arguments by default. To remove that, use the sep argument, and set it to an empty string.

However, this probably is not what you want, as the output would be something like:

O 0
O 0
S 0

...and so on.

So, do this:

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

This will be something like:

O0
O1
S2

...and so on.

Almost there. To get the desired output, we need to maintain counts of each of the elements. So, try something like this:

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="")

This should print:

O1
O2
S1

...and so on.

Comments