Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grab number after word in python

I have a huge file containing the following lines DDD-1126N|refseq:NP_285726|uniprotkb:P00112 and DDD-1081N|uniprotkb:P12121, I want to grab the number after uniprotkb.

Here's my code:

x = 'uniprotkb:P'
f = open('m.txt')
for line in f:
  print line.find(x) 
  print line[36:31 + len(x)]

The problem in line.find(x) is 10 and 26, I grab the complete number when it is 26. I'm new to programming, so I'm looking for something to grab the complete number after the word.

x = 'uniprotkb:'
f = open('m.txt')
for line in f:
  if x in line:
    print the number after x
like image 922
graph Avatar asked Sep 25 '11 21:09

graph


People also ask

How do I find the numeric value of a string in Python?

Python String isnumeric() Method The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False. Exponents, like ² and ¾ are also considered to be numeric values. "-1" and "1.5" are NOT considered numeric values, because all the characters in the string must be numeric, and the - and the .


1 Answers

Use regular expressions:

import re
for line in open('m.txt'):
    match = re.search('uniprotkb:P(\d+)', line)
    if match:
        print match.group(1)
like image 95
infrared Avatar answered Sep 28 '22 08:09

infrared