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
#16
I don't know, but I suspect, behind the scenes, within isnumeric(), re is being used anyway.

Is there another, better way to find numbers?

Why not just use re explicitly? Then you can see exactly what is going on.

import re
things = [1.5, '2.5', 3.5, '4.5', 'a.bc', 'cde. do', 2, 7, '2.A', '2.2.', '2.A2', 1.56, 2.77, '3.33.33']
my_pattern = re.compile("\d+\.\d+")
for t in things:
    match_f = re.search(my_pattern, str(t))
    print(t, match_f)
And with the output of match_f.span() you can slice the number from a string!
Clunk_Head likes this post
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 Pedroski55 - May-26-2022, 11:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 459 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 2,014 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  iterating and detecting the last Skaperen 3 1,138 Oct-01-2022, 05:23 AM
Last Post: Gribouillis
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,983 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,515 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  module detecting if imported vs not Skaperen 1 1,714 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  detecting a generstor passed to a funtion Skaperen 9 3,748 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  Python BLE Scanner not detecting device alexanderDennisEnviro500 0 2,048 Aug-01-2021, 02:29 AM
Last Post: alexanderDennisEnviro500
  Detecting power plug Narayan 2 2,769 Aug-01-2020, 04:29 AM
Last Post: bowlofred
  ValueError: could not convert string to float: RandomCoder 3 5,834 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