LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-21-2007, 07:56 AM   #1
horacioemilio
Member
 
Registered: Dec 2007
Posts: 61

Rep: Reputation: 15
Extract a number from a complicated string in python


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
 
Old 12-21-2007, 08:04 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
what have you tried? if the data is always like what you posted
Here's what i would do
1) split the line on "-"
2) get item 2
3) split item 2 on "_"
4) get item 1

there you are.

Last edited by ghostdog74; 12-21-2007 at 08:16 AM.
 
Old 12-25-2007, 10:09 PM   #3
mmattax
LQ Newbie
 
Registered: Feb 2006
Posts: 14

Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
what have you tried? if the data is always like what you posted
Here's what i would do
1) split the line on "-"
2) get item 2
3) split item 2 on "_"
4) get item 1

there you are.
He is correct...something like this:

Code:
def foo(str):
    return str.split('-')[1].split('_')[0]
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extract substring from string in C baddah Programming 6 02-02-2010 05:22 AM
Extract from stdout the string in [ ], in a nicely way frenchn00b Programming 15 11-23-2007 04:01 AM
Best/Right way to extract disk serial number? itnaa Linux - Hardware 4 08-21-2007 12:13 PM
anyway to extract a version string from a non running vmlinuz ? rcorkum Slackware 8 12-08-2006 03:33 PM
php question - how to extract line number ?? antony_csf Programming 5 09-15-2004 09:08 PM


All times are GMT -5. The time now is 01:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration