Skip to content
Advertisement

Extract column from tabular data

My task is to pull a column out of table and write down its length len (). But my code is emitting it into a column, which is why len () counts each element of the column separately, and not their total

water = water.readlines()
for col in water:
    el = list(col.split()[2])

water.txt:

     HETATM    1  H   HOH A   1      27.265  36.739  58.126
     HETATM    2  H   HOH A   1      27.109  35.124  57.944                          
     HETATM    3  O   HOH A   1      27.486  35.958  57.542
...
     HETATM 9999  O   HOH A3333      30.490  83.899  10.929

Desired intermediary output:

H
H
O
H
H
O

Advertisement

Answer

You are not correctly extracting the colum. The correct way is with a list comprehension:

with open(...) as water:
    el = [line.split()[2] for line in water]

With your sample data, I get ['H', 'H', 'O'] for el, which is the third column.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement