473,409 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

How to extract 2 integers from a string in python?

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?

Jun 9 '06 #1
4 2857
yi*****@gmail.com 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
Jun 9 '06 #2
yi*****@gmail.com 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/
Jun 9 '06 #3
Stephen Prinster <pr******@mail.com> 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 -- si***@chiark.greenend.org.uk -- 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
Jun 9 '06 #4
In article <e6**********@minji.szn.dk>,
Klaus Alexander Seistrup <kl***@seistrup.dk> wrote:
yi*****@gmail.com 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']

Jun 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ren | last post by:
Suppose I have a file containing several lines similar to this: :10000000E7280530AC00A530AD00AD0B0528AC0BE2 The data I want to extract are 8 hexadecimal strings, the first of which is E728,...
5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
4
by: grghoward | last post by:
I am receiving a series of Microsoft Word documents from web clients that they upload to my server. I need to convert them to XML to pass through to another system. I have done this through...
8
by: Amit Khemka | last post by:
Hello All, say you have some string: ", foobar " Now i want to extract all substrings for which "isinstance(eval(substr), list)" is "True" . now one way is to walk through the whole sample...
9
by: flit | last post by:
Hello All, Using poplib in python I can extract only the headers using the .top, there is a way to extract only the message text without the headers? like remove the fields below: "...
7
by: erikcw | last post by:
Hi all, I'm trying to extract zip file (containing an xml file) from an email so I can process it. But I'm running up against some brick walls. I've been googling and reading all afternoon, and...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
23
by: Summercool | last post by:
i think in Ruby, if you have an array (or list) of integers foo = you can use foo.join(",") to join them into a string "1,2,3" in Python... is the method to use ",".join() ? but then it...
1
by: Edwin.Madari | last post by:
from each line separate out url and request parts. split the request into key-value pairs, use urllib to unquote key-value pairs......as show below... import urllib line = "GET...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.