Hi,
I have a string like,
s1 = '12e3'
s2 = 'junk'
Now before converting these values to float, I want to check if they
are valid numbers.
s1.isdigit returns False.
Is there any other function which would return True for s1 and False
for s2.
Thanks
--
Suresh 4 3510
You should probably work through the tutorials.
Use a try block:
try: x=float(s)
except ValueError:
print 'Non-numeric value %s found' % s
-Larry Bates
Suresh Jeevanandam wrote: Hi, I have a string like, s1 = '12e3' s2 = 'junk'
Now before converting these values to float, I want to check if they are valid numbers.
s1.isdigit returns False.
Is there any other function which would return True for s1 and False for s2.
Thanks
-- Suresh
On Tue, Dec 20, 2005 at 07:16:46PM +0530, Suresh Jeevanandam wrote: s1 = '12e3' s2 = 'junk' Is there any other function which would return True for s1 and False for s2.
isinstance(12e3, (int, float))
--
Egbert Bouwman - Keizersgracht 197 II - 1016 DS Amsterdam - 020 6257991
================================================== ======================
On Tue, 20 Dec 2005 19:16:46 +0530, Suresh Jeevanandam wrote: Hi, I have a string like, s1 = '12e3' s2 = 'junk'
Now before converting these values to float, I want to check if they are valid numbers.
Just try converting them:
float_list = []
for s in string_list:
try:
float_list.append(float(s))
except ValueError:
# ignore bad strings
pass
do_something_with_floats(float_list)
s1.isdigit returns False.
"2" is a digit. "23" is two digits. You want something like s1.isfloat(),
but why bother checking first? Just Do It.
Is there any other function which would return True for s1 and False for s2.
From an interactive interpreter, call dir(s1). That will give you a list
of string methods. Then call help(s1.method) to learn what that method
does.
--
Steven.
On Tue, 20 Dec 2005 15:55:37 +0100, egbert wrote: On Tue, Dec 20, 2005 at 07:16:46PM +0530, Suresh Jeevanandam wrote: s1 = '12e3' s2 = 'junk' Is there any other function which would return True for s1 and False for s2.
isinstance(12e3, (int, float))
That won't work, because s1 is a *string*, not a float. The original
poster is asking how to check whether the string can be converted to a
float successfully before actually trying to convert it to a float.
--
Steven. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tom Esker |
last post by:
I've got Javascript in a form that adds up all of the numbers in a
column of form fields and displays a total. It works great if every
field has an initial value of 0, but if any of them are null,...
|
by: booksnore |
last post by:
I am writing some code to search for strings that contain every letter
of the alphabet. At the moment I am using the method below to check to
see if a string contains every letter of the alphabet....
|
by: Patient Guy |
last post by:
Does anyone have any coding rules they follow when doing argument checking?
When arguments fail during check, do you return from the call with an
ambiguous return value, or do you throw...
|
by: =?Utf-8?B?UGxheWE=?= |
last post by:
I have an if statement that isn't working correctly and I was wondering how I
check for a blank string.
My Code Example
if me.fieldname(arrayIndex) = "" then
-----
end if
When I do this and...
|
by: nishit.gupta |
last post by:
Is their any fuction available in C++ that can determine that a string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff"
Thnx
|
by: Bryan Crouse |
last post by:
I am looking a way to do error checking on a string at compile time,
and if the string isn't the correct length have then have the compiler
throw an error.
I am working an embedded software that...
|
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= |
last post by:
I need to check to see if a string contains two different constraints. I
currently use this else if statement to look for the first constraints;
else if...
|
by: Hamish |
last post by:
Hey
I'm new to python, but I have used a fair bit of C and Perl
I found Perls regex's to be very easy to use however I don't find
Pythons regexes as good.
All I am trying to do is detect if...
|
by: ningxin |
last post by:
Hi, i am currently taking a module in c++ in the university, and was given an assignment. because i have no prior background on the subject, everything is kind of new to me. i have tried for quite...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |