How to extract 2 integers from a string in python?

Y

yinglcs

Hi,

how can I extract 2 integers from a string in python?

for example, my source string is this:
Total size: 173233 (371587)

I want to extract the integer 173233 and 371587 from that soource
string, how can I do that?
 
S

Stephen Prinster

Hi,

how can I extract 2 integers from a string in python?

for example, my source string is this:
Total size: 173233 (371587)

I want to extract the integer 173233 and 371587 from that soource
string, how can I do that?

Use split() to split the string into four strings, using spaces as
separators, then use int() to convert the resulting strings that
interest you.
a, b, c, d = 'Total size: 173233 (371857)'.split()
first_int, second_int = int(c), int(d[1:-1])
first_int 173233
second_int
371857

HTH
Steve P
 
K

Klaus Alexander Seistrup

(e-mail address removed) skrev:
how can I extract 2 integers from a string in python?

for example, my source string is this:
Total size: 173233 (371587)

I want to extract the integer 173233 and 371587 from that
soource string, how can I do that?
E.g.:

#v+
import re
re.findall(r'\d+', 'Total size: 173233 (371587)') ['173233', '371587']

#v-

Mvh,
 
C

Cameron Laird

(e-mail address removed) skrev:
how can I extract 2 integers from a string in python?

for example, my source string is this:
Total size: 173233 (371587)

I want to extract the integer 173233 and 371587 from that
soource string, how can I do that?
E.g.:

#v+
import re
re.findall(r'\d+', 'Total size: 173233 (371587)') ['173233', '371587']
.
.
.
Nicely expressed.

People doing a lot of report-scraping might also want to
consider one of the scanf implementations for Python.
<URL: http://hkn.eecs.berkeley.edu/~dyoo/python/scanf/ >
has an example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top