473,509 Members | 3,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about extracting value from a string

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 '05 #1
4 1085
<ne*****@xit.net> 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
Dec 7 '05 #2
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 '05 #3
<ne*****@xit.net> 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
Dec 7 '05 #4
>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 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2971
by: Steve | last post by:
Hi, I have a very long string, someting like: DISPLAY=localhost:0.0,FORT_BUFFERED=true, F_ERROPT1=271\,271\,2\,1\,2\,2\,2\,2,G03BASIS=/opt/g03b05/g03/basis,...
2
2808
by: Dickyb | last post by:
Extracting an Icon and Placing It On The Desktop (C# Language) I constructed a suite of programs in C++ several years ago that handle my financial portfolio, and now I have converted them to...
4
1309
by: Tony | last post by:
how do I check if the data in the session object is null before extracting it. The following throws an error if the Session object has not already been created. string str =...
29
3537
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
13
3717
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
2
1368
by: Terrance | last post by:
Hello, I have a question in regards to hashtables that I was hoping someone can share some light on. I'm currently working with VB.net and I'm trying to learn as much as possible about the...
3
1482
by: al jones | last post by:
I have the following line of code in a procedure (Function SearchDesigner(ByVal stringreader As String) As String) If LCase(stringreader) Like "*" & LCase(arDesigner(i, 0)) & "*" Then...
6
1675
by: sunil | last post by:
I have a button named Button1, and I wrote an event handler for the OnClick event. protected void Button1_Click(object sender, System.EventArgs e) { this.Response.Redirect("Default.aspx?q=" +...
4
3759
by: dexter48 | last post by:
Hi I'm searching for a string occurance in a text file. I find the string ok and write the results to a log file. But on the line above is also some information I need. How can i get that. The string...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.