PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 17, 2022

[FIXED] How to extract only integer part from a string in Python?

 October 17, 2022     integer, list, python, split, string     No comments   

Issue

I would like to extract only the numbers contained in a string. Can isdigit() and split() be combined for this purpose or there is simpler/faster way?

Example:

m = ['How to extract only number 122', 'The number 35 must be extracted', '1052 must be extracted']

Output:

numbers = [122, 35, 1052]
text = ['How to extract only number', 'The number must be extracted', 'must be extracted']

My code:

text = []
numbers = []
temp_numbers = []
for i in range(len(m)):
    text.append([word for word in m[i].split() if not word.isdigit()])
    temp_numbers.append([int(word) for word in m[i].split() if word.isdigit()])
for i in range(len(m)):
    text[i] = ' '.join(text[i])
for elem in temp_numbers:
    numbers.extend(elem)

print(text)
print(numbers)

Solution

Import regex library:

import re

If you want to extract all digits:

numbers = []
texts = []
for string in m:
    numbers.append(re.findall("\d+", string))
    texts.append(re.sub("\d+", "", string).strip())

If you want to extract only first digit:

numbers = []
texts = []
for string in m:
    numbers.append(re.findall("\d+", string)[0])
    texts.append(re.sub("\d+", "", string).strip())


Answered By - A259
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing