Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > question about extracting value from a string

Reply
Thread Tools

question about extracting value from a string

 
 
nephish@xit.net
Guest
Posts: n/a
 
      12-07-2005
hey there,

i have looked at the string module and re.
i was looking for advice on what would be the best way to pull a value
out of a small string.

for example, i have a string
$.+.09 JAR
and all i want out of it is the +.09

likewise, i have
$-.04 TIN kt
and all i want is the -.04

what would be my best bet here ?
do i need a full blown re comparison?

thanks

 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      12-07-2005
<(E-Mail Removed)> wrote:

> hey there,
>
> i have looked at the string module and re.
> i was looking for advice on what would be the best way to pull a value
> out of a small string.
>
> for example, i have a string
> $.+.09 JAR
> and all i want out of it is the +.09
>
> likewise, i have
> $-.04 TIN kt
> and all i want is the -.04
>
> what would be my best bet here ?
> do i need a full blown re comparison?


You can do it by pure string operations, if you wish -- e.g., if your
examples exhaust the possible issues, astring[1:5] will work for both.
It's hard to say whether it's best to use REs instead without having any
idea of the variety of formats your "value" can take and how differently
it might be placed inside the string, of course.

Once you do get the value, either by string slicing or REs, you'll have
a string -- if what you want is a float, call e.g. float(astring[1:5]);
or for a decimal number, decimal.Decimal(astring[1:5]) (after importing
module decimal from Python's standard library); and so on.


Alex
 
Reply With Quote
 
 
 
 
nephish@xit.net
Guest
Posts: n/a
 
      12-07-2005
the second line was not a typo , and thanks for that.
these values may vary somewhat over time. So i may have to rewrite this
part of the script.

a string is fine for right now, because it is just going to be pumped
into a database. i can change it later if necessary.

thanks for the help gents, i am going to just go with string functions,
seems like it will be easier both to code now. and later if and when
the format of this stuff may change.

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      12-07-2005
<(E-Mail Removed)> wrote:
...
> thanks for the help gents, i am going to just go with string functions,
> seems like it will be easier both to code now. and later if and when
> the format of this stuff may change.


I agree -- "to each day are sufficient the evils thereof". Many
programmers fall into the temptation to overgeneralize and fail to
follow the AGNI principle ("Ain't Gonna Need It"....


Alex
 
Reply With Quote
 
nephish@xit.net
Guest
Posts: n/a
 
      12-07-2005
>I agree -- "to each day are sufficient the evils thereof". Many
>programmers fall into the temptation to overgeneralize and fail to
>follow the AGNI principle ("Ain't Gonna Need It"....


thats funny

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting a String value with ANT Koos Pol Java 10 05-28-2008 09:23 AM
Howto identify a string value vs. a numeric value in std::string frohlinger@gmail.com C++ 9 09-19-2007 09:34 AM
Extracting a numeric value from a string. silverfish Perl Misc 7 02-10-2006 08:24 AM
cookielib extracting a cookie value as a string dmbkiwi@gmail.com Python 0 01-28-2006 02:14 AM
String.value and String.value.length doesn't works together in IE with JVM Wee Javascript 1 07-16-2003 12:32 PM



Advertisments