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
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 .
Use regular expressions:
import re
for line in open('m.txt'):
match = re.search('uniprotkb:P(\d+)', line)
if match:
print match.group(1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With