Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to extract 2 integers from a string in python?

Reply
Thread Tools

How to extract 2 integers from a string in python?

 
 
yinglcs@gmail.com
Guest
Posts: n/a
 
      06-09-2006
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?

 
Reply With Quote
 
 
 
 
Stephen Prinster
Guest
Posts: n/a
 
      06-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
 
 
 
Klaus Alexander Seistrup
Guest
Posts: n/a
 
      06-09-2006
(E-Mail 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,

--
Klaus Alexander Seistrup
Copenhagen, Denmark
http://surdej.dk/
 
Reply With Quote
 
Sion Arrowsmith
Guest
Posts: n/a
 
      06-09-2006
Stephen Prinster <(E-Mail Removed)> wrote:
>>>> a, b, c, d = 'Total size: 173233 (371857)'.split()
>>>> first_int, second_int = int(c), int(d[1:-1])


int(d[1:-1]) can be replaced by d.strip("()"), which may or
may not be clearer in intent.

--
\S -- (E-Mail Removed) -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
 
Reply With Quote
 
Cameron Laird
Guest
Posts: n/a
 
      06-09-2006
In article <e6b02t$2u2$(E-Mail Removed)>,
Klaus Alexander Seistrup <(E-Mail Removed)> wrote:
>(E-Mail 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Check if String.matches() AND (if yes) extract number from String? Jochen Brenzlinger Java 5 11-21-2011 07:43 PM
How to extract an std::string from another std::string? Dwight Army of Champions C++ 0 04-20-2010 09:59 AM
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
XSLT - Extract leading integers from text string grghoward@no.spam.email.com.no.spam XML 4 03-03-2005 02:41 PM



Advertisments