跳到主要内容

Deep Dive into Finding Numbers in Strings with Python

· 阅读需 6 分钟

Introduction​

Handling strings that contain a mix of numbers and text is a common task in everyday programming. For example, you might need to extract numbers from complex text or identify whether certain strings contain numbers. In Python, there are various ways to tackle this problem, depending on your specific needs. This article will detail how to find numbers in strings using Python, providing concrete example code to help developers quickly master these techniques.


Why Find Numbers in Strings?​

  • Data Processing and Cleaning: Extract useful numeric information from web crawlers or log files, such as order numbers, timestamps, amounts, etc.
  • Input Validation: Detect whether user input strings contain numbers to ensure the validity of the input content.
  • Text Analysis: Extract numbers from text data for further data analysis or statistics.

Using String's isnumeric() Method​

Python's string type (str) provides several methods to determine character types, one of which is isnumeric(). The isnumeric() method checks whether a string contains only numeric characters. If all characters in the string are numeric, the method returns True; otherwise, it returns False.

string = "12345"
if string.isnumeric():
print(f"'{string}' contains only numbers")
else:
print(f"'{string}' contains more than just numbers")

In this example, isnumeric() checks if the string "12345" consists entirely of numeric characters. Although this method can determine if a string is entirely numeric, it cannot extract numbers from mixed strings.


Using Regular Expressions to Find Numbers​

Regular expressions (regex) are a powerful tool for text processing, ideal for finding and extracting numbers from strings. Python's re module provides comprehensive regex support, making it easy to match numbers in strings.

Example: Using Regular Expressions​

import re

# Define a string containing numbers
text = "Order number is 12345, amount is 678.90 dollar"

# Use regex to find all numbers
numbers = re.findall(r'\d+', text)

print(f"Numbers found in the string: {numbers}")

In this example, re.findall() uses the regex \d+, which matches one or more consecutive digits. re.findall() returns a list containing all matched numbers in the string. The output is: ['12345', '678'].


Finding Numbers with Decimal Points​

If you need to find numbers with decimal points in a string, you can use the following regex:

import re

text = "Order number is 12345, amount is 678.90 dollar"

# Find integers and decimals
numbers = re.findall(r'\d+\.?\d*', text)

print(f"Numbers found in the string: {numbers}")

In this regex, \d+ matches one or more digits, \. matches the decimal point, and \d* matches zero or more digits following the decimal point. The result is: ['12345', '678.90'].


Using List Comprehension to Extract Numbers​

For simple scenarios, you can use Python's list comprehension to quickly extract numbers from a string. By checking each character in the string to see if it is a digit, you can extract them.

Example: Using List Comprehension​

text = "Order number 12345, amount 678.90 dollar"

# Extract digit characters
digits = [char for char in text if char.isdigit()]

print(f"Extracted digit characters: {digits}")

In this example, isdigit() is used to check each character in the string, resulting in a list of all digit characters: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']. If you need to combine the extracted digits into a complete numeric string, you can use the join() method:

# Combine extracted digit characters into a complete numeric string
number_str = ''.join(digits)
print(f"Complete numeric string: {number_str}")

The output is: "1234567890".


Filtering Numbers with filter() Function​

Python's built-in filter() function can be used to filter out non-numeric characters from a string, thereby extracting numeric characters. The filter() function works similarly to list comprehension but is more functional in style.

Example: Using filter() Function​

text = "Order number 12345, amount 678.90 dollar"

# Use the filter function to extract numbers
numbers = ''.join(filter(str.isdigit, text))

print(f"Extracted numbers: {numbers}")

In this example, the filter() function checks each character in the string, keeping only those that satisfy isdigit(), and then joins the extracted numeric characters into a complete string.


Splitting Strings to Extract Numbers​

In some scenarios, you can split the string first and then process each segment to extract numbers. The split() method can split a string into multiple parts based on a specified delimiter, suitable for handling structured text.

Example: Using split() Method​

import re

text = "Order number: 12345, amount: 678.90"

# Split the string by spaces or punctuation
parts = re.split(r'[,:: ]+', text)

# Extract numeric parts
numbers = [part for part in parts if part.replace('.', '', 1).isdigit()]

print(f"Extracted numbers: {numbers}")

In this example, re.split() is used to split the string, and then list comprehension extracts all numeric parts. replace('.', '', 1) ensures that decimal points are handled correctly.


Using ast.literal_eval to Parse Numbers​

In some applications, you may need to extract numeric values from complex strings and parse them into numeric types (integers or floats). Python's ast.literal_eval() method can safely parse a string into a Python data type.

Example: Using ast.literal_eval​

import ast
import re

text = "Amount: 678.90"

# Extract the numeric part of the string
number_str = re.search(r'\d+\.?\d*', text).group()

# Parse the string into a number
number = ast.literal_eval(number_str)

print(f"Parsed number: {number}")

In this example, the regex extracts the numeric part, and ast.literal_eval() converts it into the actual numeric type (float). This method is useful for extracting and converting numbers from text.


Conclusion​

Finding and extracting numbers from strings is a common task in Python programming, whether for data cleaning, user input validation, or text analysis. This article has introduced various methods to find numbers in Python, including using string methods, regular expressions, list comprehension, the filter() function, and ast.literal_eval(). Each method has its strengths and can be chosen based on different application scenarios. With these tools, developers can quickly and efficiently extract and process numeric data from strings, enhancing code flexibility and processing capabilities.