Python Forum
Detecting float or int in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detecting float or int in a string
#14
(May-23-2022, 08:49 PM)Clunk_Head Wrote: Can you please provide an example where an exception must be used in place of anything else?

Python documentation, glossary:

Quote:EAFP
Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.

There are many countries out there with different notations. 1.000,42 and 1,000.42 are perfectly 'legal' and equal numbers, just depending your locale.

There is ast.literal_eval() which can be used (locale thingy still applies):

>>> import ast
>>> ast.literal_eval('1.0')
1.0
>>> ast.literal_eval('+3')
3
>>> ast.literal_eval('-42')
-42
>>> type(ast.literal_eval('-42'))
<class 'int'>
If you feed invalid value then you get SyntaxError:

>>> ast.literal_eval('1.0.0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 62, in literal_eval
    node_or_string = parse(node_or_string.lstrip(" \t"), mode='eval')
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    1.0.0
       ^^
SyntaxError: invalid syntax
You can use EAFP style with try to literally evaluate value, catch syntax error if it's raised and do something about it
Gribouillis and Clunk_Head like this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Detecting float or int in a string - by Clunk_Head - May-23-2022, 08:39 PM
RE: Detecting float or int in a string - by perfringo - May-24-2022, 04:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 554 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 2,042 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  iterating and detecting the last Skaperen 3 1,158 Oct-01-2022, 05:23 AM
Last Post: Gribouillis
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 5,050 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,557 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  module detecting if imported vs not Skaperen 1 1,732 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  detecting a generstor passed to a funtion Skaperen 9 3,791 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  Python BLE Scanner not detecting device alexanderDennisEnviro500 0 2,072 Aug-01-2021, 02:29 AM
Last Post: alexanderDennisEnviro500
  Detecting power plug Narayan 2 2,797 Aug-01-2020, 04:29 AM
Last Post: bowlofred
  ValueError: could not convert string to float: RandomCoder 3 5,879 Jul-27-2020, 07:38 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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