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

后端 未结 4 1229
情话喂你
情话喂你 2021-01-07 06:31

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,

相关标签:
4条回答
  • 2021-01-07 07:13

    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'
    
    0 讨论(0)
  • 2021-01-07 07:14

    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])
    
    0 讨论(0)
  • 2021-01-07 07:21

    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]
    
    0 讨论(0)
  • 2021-01-07 07:38

    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]
    
    0 讨论(0)
提交回复
热议问题