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

ε祈祈猫儿з 提交于 2019-11-30 20:48:13

问题


I'm trying to process a file from the protein data bank which is separated by spaces (not \t). I have a .txt file and I want to extract specific rows and, from that rows, I want to extract only a few columns.

I need to do it in Python. I tried first with command line and used awk command with no problem, but I have no idea of how to do the same in Python.

Here is an extract of my file:

[...]
SEQRES   6 B   80  ALA LEU SER ILE LYS LYS ALA GLN THR PRO GLN GLN TRP          
SEQRES   7 B   80  LYS PRO                                                      
HELIX    1   1 THR A   68  SER A   81  1                                  14    
HELIX    2   2 CYS A   97  LEU A  110  1                                  14    
HELIX    3   3 ASN A  122  SER A  133  1                                  12    
[...]

For example, I'd like to take only the 'HELIX' rows and then the 4th, 6th, 7th and 9th columns. I started reading the file line by line with a for loop and then extracted those rows starting with 'HELIX'... and that's all.

EDIT: This is the code I have right now, but the print doesn't work properly, only prints the first line of each block (HELIX SHEET AND DBREF)

#!/usr/bin/python
import sys

for line in open(sys.argv[1]):
 if 'HELIX' in line:
   helix = line.split()
 elif 'SHEET'in line:
   sheet = line.split()
 elif 'DBREF' in line:
   dbref = line.split()

print (helix), (sheet), (dbref)

回答1:


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'



回答2:


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]



回答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])



回答4:


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]


来源:https://stackoverflow.com/questions/25768230/how-to-extract-specific-columns-from-a-space-separated-file-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!