473,325 Members | 2,816 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,325 software developers and data experts.

Extract a number from a complicated string

Hi,

I have to read some data from a file, and on each block it always
appears the followng string; xyz.vs.1-81_1 . It appears a lot of time
with different numbers like;

xyz.vs.1-81_1
xyz.vs.1-1234_1
xyz.vs.1-56431_1

and so on

My problem is that I need to extract from this string the number. For
instance in xyz.vs.1-81_1 I have to extract the number 81, and in
xyz.vs.1-1234_1 I need to get the number 1234.

What is the easiest way of doing this ?

Thanks
Dec 21 '07 #1
4 2595
Horacius ReX wrote:
>Hi,

I have to read some data from a file, and on each block it always
appears the followng string; xyz.vs.1-81_1 . It appears a lot of time
with different numbers like;

xyz.vs.1-81_1
xyz.vs.1-1234_1
xyz.vs.1-56431_1

and so on

My problem is that I need to extract from this string the number. For
instance in xyz.vs.1-81_1 I have to extract the number 81, and in
xyz.vs.1-1234_1 I need to get the number 1234.

What is the easiest way of doing this ?

Thanks

If the strings looks *allways* that way, so its not to complicated:
>>number_regex = re.compile('-(\d+)_')
print number_regex.search('xyz.vs.1-1234_1').group(1)
1234
>>print number_regex.search('xyz.vs.1-56431_1').group(1)
56431

Dec 21 '07 #2
Horacius ReX a écrit :
Hi,

I have to read some data from a file, and on each block it always
appears the followng string; xyz.vs.1-81_1 . It appears a lot of time
with different numbers like;

xyz.vs.1-81_1
xyz.vs.1-1234_1
xyz.vs.1-56431_1

and so on

My problem is that I need to extract from this string the number. For
instance in xyz.vs.1-81_1 I have to extract the number 81, and in
xyz.vs.1-1234_1 I need to get the number 1234.

What is the easiest way of doing this ?
Not necessarily the "easiest", but:

>>data = ['xyz.vs.1-81_1', 'xyz.vs.1-1234_1', 'xyz.vs.1-56431_1']
for d in data:
.... print d, ":", d.split('.')[2].split('-')[1].split('_')[0]
....
xyz.vs.1-81_1 : 81
xyz.vs.1-1234_1 : 1234
xyz.vs.1-56431_1 : 56431
Dec 21 '07 #3
On 2007-12-21, Horacius ReX <ho**********@gmail.comwrote:
Hi,

I have to read some data from a file, and on each block it always
appears the followng string; xyz.vs.1-81_1 . It appears a lot of time
with different numbers like;

xyz.vs.1-81_1
xyz.vs.1-1234_1
xyz.vs.1-56431_1

and so on

My problem is that I need to extract from this string the number. For
instance in xyz.vs.1-81_1 I have to extract the number 81, and in
xyz.vs.1-1234_1 I need to get the number 1234.

What is the easiest way of doing this ?
Using a regular expression would be quick if you know how.

Or use str.find and slicing.

--
Neil Cerutti
Dec 21 '07 #4
Gerardo Herzig <gh*****@fmed.uba.arwrote:
>>My problem is that I need to extract from this string the number. For
instance in xyz.vs.1-81_1 I have to extract the number 81, and in
xyz.vs.1-1234_1 I need to get the number 1234.

What is the easiest way of doing this ?
If the strings looks *allways* that way, so its not to complicated:
>number_regex = re.compile('-(\d+)_')
[ ... ]
If the strings always look *exactly* like that, a regex is a massive
overkill:
>>examples = 'xyz.vs.1-81_1', 'xyz.vs.1-1234_1', 'xyz.vs.1-56431_1'
[ int(x[9:-2]) for x in examples ]
[81, 1234, 56431]

If the "xyz.vs.1" and trailing "1" are a bit more variable, then a
regex is still massive overkill:
>>[ int(x[x.index('-')+1:x.rindex('_')]) for x in examples ]
[81, 1234, 56431]

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Dec 21 '07 #5

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

Similar topics

7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
5
by: chuck | last post by:
Hello, I am trying to figure out how to get a number (always an int) from a string. so if i have something like <input type="text" id="product13" name="product13" onblur="return...
9
by: Sharon | last post by:
hi, I want to extract a string from a file, if the file is like this: 1 This is the string 2 3 4 how could I extract the string, starting from the 10th position (i.e. "T") and...
3
by: Adam Faulkner via DotNetMonster.com | last post by:
I want to create a method within a class that opens a Microsoft Word 2000 Document and has the facility to Create a new word document and then extract a Page that exists within the original Word...
6
by: Dave | last post by:
Hope someone can help! I have a memo fiels in which there are a few numbers including dates but what I want to do is extract a number which is 6 figures long. Can anyone help me? Thanks Dave
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...
15
by: Lyosha | last post by:
Converting binary to base 10 is easy: 255 Converting base 10 number to hex or octal is easy: '0144' '0x64' Is there an *easy* way to convert a number to binary?
7
by: JoeC | last post by:
I am trying to create a windows program that reads binary graphics as a resource. This has nothing to do with win32 but conversion of data with memcpy. graphic::graphic(UINT uiResID, HINSTANCE...
5
by: Steve | last post by:
Hi all Does anybody please know a way to extract an Image from a pdf file and save it as a TIFF? I have used a scanner to scan documents which are then placed on a server, but I need to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.