Extract a number from a complicated string

H

Horacius ReX

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
 
B

Bruno Desthuilliers

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
 
G

Gerardo Herzig

Horacius said:
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:56431
 
N

Neil Cerutti

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.
 
S

Sion Arrowsmith

Gerardo Herzig said:
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:[ ... ]

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top