如何在python中逐字抓取数字 [英] How to grab number after word in python
本文介绍了如何在python中逐字抓取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个巨大的文件,其中包含以下几行 DDD-1126N|refseq:NP_285726|uniprotkb:P00112
和 DDD-1081N|uniprotkb:P12121
,我想抓取uniprotkb
后面的数字.
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
.
这是我的代码:
x = 'uniprotkb:P'
f = open('m.txt')
for line in f:
print line.find(x)
print line[36:31 + len(x)]
line.find(x)
中的问题是 10 和 26,当它是 26 时我抓取完整的数字.我是编程新手,所以我正在寻找可以抓取的东西单词后的完整数字.
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
推荐答案
使用正则表达式:
import re
for line in open('m.txt'):
match = re.search('uniprotkb:P(\d+)', line)
if match:
print match.group(1)
这篇关于如何在python中逐字抓取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文