question about extracting value from a string

Discussion in 'Python' started by nephish@xit.net, Dec 7, 2005.

  1. Guest

    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
     
    , Dec 7, 2005
    #1
    1. Advertising

  2. <> 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
     
    Alex Martelli, Dec 7, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , Dec 7, 2005
    #3
  4. <> 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
     
    Alex Martelli, Dec 7, 2005
    #4
  5. Guest

    >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 :)
     
    , Dec 7, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    607
  2. Replies:
    9
    Views:
    631
    James Kanze
    Sep 19, 2007
  3. Koos Pol
    Replies:
    10
    Views:
    3,783
    Koos Pol
    May 28, 2008
  4. Wee
    Replies:
    1
    Views:
    163
    Richard Cornford
    Jul 16, 2003
  5. silverfish

    Extracting a numeric value from a string.

    silverfish, Feb 8, 2006, in forum: Perl Misc
    Replies:
    7
    Views:
    122
Loading...

Share This Page