Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract numbers from string
#1
I need to find way to extract numbers from a string but the following not quick right. I need to get [98.15, 0.97, 1.00, 118506]

import re
a = 'C.1 str  98.15  +0.97increase  +1.00%increase  118,506  04/27/18'
re.findall(r"([\d.]*\d+)", a)
['.1', '98.15', '0.97', '1.00', '118', '506', '04', '27', '18']
Reply
#2
something like this.
>>> import re
>>> 
>>> s = 'C.1 str  98.15  +0.97increase  +1.00%increase  118,506  04/27/18'
>>> lst = re.findall(r'\d+\.\d+|\d+\,\d+', s)
>>> lst
['98.15', '0.97', '1.00', '118,506']
>>> [c.replace(',', '') for c in lst]
['98.15', '0.97', '1.00', '118506']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 491 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Pulling Specifics Words/Numbers from String bigpapa 2 723 May-01-2023, 07:22 PM
Last Post: bigpapa
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,427 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  Find and Replace numbers in String giddyhead 2 1,197 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Extract a string between 2 words from a text file OscarBoots 2 1,826 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  How to extract specific key value pair from string? aditi06 0 2,483 Apr-15-2021, 06:26 PM
Last Post: aditi06
Question How to extract multiple text from a string? chatguy 2 2,312 Feb-28-2021, 07:39 AM
Last Post: bowlofred
  Extract continuous numeric characters from a string in Python Robotguy 2 2,580 Jan-16-2021, 12:44 AM
Last Post: snippsat
  extract a dictionary from a string berc 4 2,784 Jul-30-2020, 06:58 AM
Last Post: berc
  Extract data from large string pzig98 1 2,095 Jul-20-2020, 12:39 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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