How to extract specific columns from a space separated file in Python?

无人久伴 提交于 2019-12-01 01:15:43

If you already have extracted the line, you can split it using line.split(). This will give you a list, of which you can extract all the elements you need:

>>> test='HELIX 2 2 CYS A 97'
>>> test.split()
['HELIX', '2', '2', 'CYS', 'A', '97']
>>> test.split()[3]
'CYS'

Have a look at the CSV library. https://docs.python.org/2/library/csv.html The following code should do the trick

>>> import csv
>>> with open('my-file.txt', 'rb') as myfile:
...     spamreader = csv.reader(myfile, delimiter=' ', )
...     for row in spamreader:
...         print row[3]

Is there a reason you can't just use split?

for line in open('myfile'):
  if line.startswith('HELIX')
    cols = line.split(' ')
    process(cols[3], cols[5], cols[6], cols[8])

you can expend the key words as you want. the result is list contained line with key words you can do further process of result to get what you want

with open("your file") as f:
     keyWords = ['HELIX','SHEET','DBREF']
     result = [ line  for line in f for key in keyWords if key in line]
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!