removing text string

A

Ahmed, Shakir

I need to remove text string from the list of the numbers mentioned
below:

080829-7_A
070529-5_c
080824-7_O
070405_6_p

The output will be : 080829-7
070529-5
080824-7
070405-6

Any idea is highly appreciated.
 
A

Ahmed, Shakir

Thanks

Actually the number I am getting it is from slicing from a long text
line. I need to slice 10 characters from that line but no string only
numeric numbers. When I am slicing 10 characters those A, c, O is coming
at the end.

Thanks


-----Original Message-----
From: [email protected]
[mailto:p[email protected]] On Behalf Of
Dennis Lee Bieber
Sent: Wednesday, September 10, 2008 3:45 PM
To: (e-mail address removed)
Subject: Re: removing text string

I need to remove text string from the list of the numbers mentioned
below:

080829-7_A
070529-5_c
080824-7_O
070405_6_p
? Is that last one an error that is supposed to be
....5-6_, not
....5_6_ ?

1) If the required data is fixed width, just slice it

out = inp[:8]

2) If the data is variable width but has a fixed delimiter,
find
the delimiter position and then slice (this is the reason for my
question above -- this method) OR just split on the delimiter and take
the part you need.

out = inp.split("_")[0]

3) If the data is more complex (again if that last line
with two _
is supposed to be trimmed after the second, and the first turned into a
-) you will need to fully define the parsing rules of the data.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
B

bearophileHUGS

Ahmed, Shakir:
Actually the number I am getting it is from slicing from a long text
line. I need to slice 10 characters from that line but no string only
numeric numbers. When I am slicing 10 characters those A, c, O is coming
at the end.

It's better to avoid Regular expressions when they aren't necessary,
but once in a while they are the simpler way to solve a problem, you
may create one like this (mostly untested):

\d+?-\d+

Using it like:
'080829-7'

Learning Regexps can be useful.

Bye,
bearophile
 
A

Ahmed, Shakir

-----Original Message-----
From: [email protected]
[mailto:p[email protected]] On Behalf Of
(e-mail address removed)
Sent: Thursday, September 11, 2008 8:54 AM
To: (e-mail address removed)
Subject: Re: removing text string

Ahmed, Shakir:
Actually the number I am getting it is from slicing from a long text
line. I need to slice 10 characters from that line but no string only
numeric numbers. When I am slicing 10 characters those A, c, O is coming
at the end.

It's better to avoid Regular expressions when they aren't necessary,
but once in a while they are the simpler way to solve a problem, you
may create one like this (mostly untested):

\d+?-\d+

Using it like:
'080829-7'

Learning Regexps can be useful.

Bye,
Bearophile


Thanks, I got the clue.

Very much appreciated.
 

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,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top