473,609 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2616
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_rege x = re.compile('-(\d+)_')
print number_regex.se arch('xyz.vs.1-1234_1').group( 1)
1234
>>print number_regex.se arch('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**********@g mail.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.u ba.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.gr eenend.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
3595
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 example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
5
28813
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 somefunc(this);" /> then have a function somefunc(element){
9
16963
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 extract 35 characters (including "T") from a file and then go to next line?
3
30773
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 Document and save it to a new Word Document. I would need to generate a loop for each page found within a word document to create a new word document and insert the existing page into the new word document and then save as a new word document. ...
6
5953
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
2040
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 word and number arguments like this: GRILLE EURAT5 Coin Nord-Ouest : 46.50/ 0.50 Coin Sud-E Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file.
15
8333
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
2103
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 hinstance){ size = 32; bitData.clear(); void * p = NULL; // point to the data int end; BYTE data;
5
5751
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 extract the image of the document (just the first page if there are multiple pages) and save it as a TIFF so I can then use the Tesseract OCR to get the text in the image.
0
8139
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8091
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8555
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7024
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6064
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5524
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4032
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.