Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracts all the number before percentage in Python?

Tags:

python

regex

I have a string like this:

receiving incremental file list genelaytics/ genelaytics/.project 421 3% 411.13kB/s 0:00:00 421 3% 411.13kB/s 0:00:00 (xfr#1, to-chk=13/15) 421 3% 411.13kB/s 0:00:00 (xfr#1, to-chk=8/15) genelaytics/.pydevproject 1,006 7% 982.42kB/s 0:00:00 (xfr#2, to-chk=12/15) genelaytics/hello.py 1,006 7% 982.42kB/s 0:00:00 (xfr#3, to-chk=11/15) genelaytics/manage.py 1,260 10% 1.20MB/s 0:00:00 (xfr#4, to-chk=10/15) genelaytics/ok.py 1,260 10% 1.20MB/s 0:00:00 (xfr#5, to-chk=9/15) genelaytics/genelaytics/ genelaytics/genelaytics/__init__.py 1,260 10% 35.16kB/s 0:00:00 (xfr#6, to-chk=7/15) genelaytics/genelaytics/__init__.pyc 1,399 11% 39.03kB/s 0:00:00 (xfr#7, to-chk=6/15) genelaytics/genelaytics/settings.py 6,416 50% 179.02kB/s 0:00:00 (xfr#8, to-chk=5/15) genelaytics/genelaytics/settings.pyc 9,468 75% 264.17kB/s 0:00:00 (xfr#9, to-chk=4/15) genelaytics/genelaytics/urls.py 9,813 77% 252.18kB/s 0:00:00 (xfr#10, to-chk=3/15) genelaytics/genelaytics/urls.pyc 10,409 82% 260.64kB/s 0:00:00 (xfr#11, to-chk=2/15) genelaytics/genelaytics/wsgi.py 11,553 91% 289.29kB/s 0:00:00 (xfr#12, to-chk=1/15) genelaytics/genelaytics/wsgi.pyc 12,596 100% 315.40kB/s 0:00:00 (xfr#13, to-chk=0/15) 12,596 100% 33.70kB/s 0:00:00 (xfr#13, to-chk=0/15) 12,596 100% 30.15kB/s 0:00:00 (xfr#13, to-chk=0/15) sent 287 bytes received 6,709 bytes 518.22 bytes/sec total size is 12,596 speedup is 1.80

I want to extract all the number before percentage:

3%, 3%, 7%, 10%, 75%, 82% and all.

Tried using:

re.search('\d*%',test).group()

But this extracts only the first percentage number 3%.

I want all the numbers. How can I do that? Thanks

like image 862
user1881957 Avatar asked Jan 15 '23 17:01

user1881957


1 Answers

Use findall:

In [58]: re.findall(r'\d+%', text)
Out[58]: 
['3%', '3%', '3%', '7%', '7%', '10%', '10%', '10%', '11%', '50%', '75%', '77%',
'82%', '91%', '100%', '100%', '100%']

Also, you may wish to use \d+ instead of \d* so the pattern does not match a stray % which is not preceded by a number.

like image 189
unutbu Avatar answered Jan 19 '23 00:01

unutbu