Python Forum
test if variable is Numeric?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
test if variable is Numeric?
#13
This is my take - form a production code (I use it to load ini files - thus the name)

def convert_config_value(value):
    for numeric_type in (int, float):
        try:
            converted_value = numeric_type(value)
            return converted_value
        except ValueError:
            pass
    return value

Ooops, seems like I misread the question (I saw the post by @ljmetzger - and misinterpreted the OP). Of course, this a method for string conversion.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
test if variable is Numeric? - by ilcaa72 - May-25-2018, 01:08 AM
RE: test if variable is Numeric? - by scidam - May-25-2018, 01:13 AM
RE: test if variable is Numeric? - by ilcaa72 - May-25-2018, 01:18 AM
RE: test if variable is Numeric? - by scidam - May-25-2018, 01:35 AM
RE: test if variable is Numeric? - by buran - May-25-2018, 06:23 AM
RE: test if variable is Numeric? - by scidam - May-25-2018, 07:05 AM
RE: test if variable is Numeric? - by nilamo - Jun-22-2018, 04:25 PM
RE: test if variable is Numeric? - by buran - May-25-2018, 07:07 AM
RE: test if variable is Numeric? - by wavic - May-25-2018, 07:13 AM
RE: test if variable is Numeric? - by buran - May-25-2018, 07:16 AM
RE: test if variable is Numeric? - by wavic - May-25-2018, 07:20 AM
RE: test if variable is Numeric? - by ljmetzger - Jun-22-2018, 06:09 PM
RE: test if variable is Numeric? - by volcano63 - Jun-22-2018, 08:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 9 772 Mar-29-2024, 06:15 PM
Last Post: idev
Question Numeric Anagrams - Count Occurances monty024 2 1,574 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 2,066 Nov-06-2021, 03:26 PM
Last Post: snippsat
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,263 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  Extract continuous numeric characters from a string in Python Robotguy 2 2,712 Jan-16-2021, 12:44 AM
Last Post: snippsat
  How to calculate column mean and row skip non numeric and na Mekala 5 5,089 May-06-2020, 10:52 AM
Last Post: anbu23
  Alpha numeric element list search rhubarbpieguy 1 1,839 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,186 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,610 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  convert a character to numeric and back Skaperen 2 2,174 Jan-28-2020, 09:32 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020