简体   繁体   中英

Replace function to remove everything but numbers and decimal point in python

I want to remove everything from a string except the numbers and the decimal point in python without any external lib.

I have a list of strings that contain lengths, widths and heights with the following pattern.

Note: Some strings might contain a random amount of spaces

My demo strings with their patterns

length_string = " length 0.7l"
width_string = " width 0.5w"
height_string = "   height 1h"

What I have tried so far:

# The code below is not working due to the pattern of the characters.
length_number = length_string.replace(" length ", "").rstrip("l")
width_number = width_string.replace(" width ", "").rstrip("w")
height_number = height_string.replace("   height ", "").rstrip("h")

Desired result:

length_number = "0.7"
width_number = "0.5"
height_number = "1"

This looks like a job for regular expressions :

import re

def remove_chars(entry):
    x = re.sub('[a-zA-Z\s]', '', entry)
    return(x)

length_string = " length 0.7l"
width_string = " width 0.5w"
height_string = "   height 1h"

remove_chars(length_string)
#0.7
remove_chars(width_string)
#0.5
remove_chars(height_string)
#1

I would suggest you spend a bit of time reading up on regular expressions to make sure you understand how they work so that if an exception to the rules you've shown comes up you can start to try to handle it, but basically the code above replaces any alphabetic characters ( a-zA-z ) or white spaces ( \s ) with nothing '' . If you have other special characters like % or ; or @ , basically anything that isn't a letter or space, or if you have other numbers in your string, you won't get back anything useful the way the current regex is written.

I'm guessing you also don't want to use regular expressions, which are built into python and would be perfect for this approach.

This would be my approach without any library, you simply have a list with all valid characters (or non-valid if you put a not in the if) and iterate through the string.

   valid_chars = [str(i) for i in range(11)] + ["."]
   new_string = ""
   sample = " length 0.7l"
   for char in sample:
    if char in valid_chars:
        new_string += char

Pulls out number or numbers with string

length_string = " length 0.7l"
width_string = " width 0.5w"
height_string = "   height 1h"
length_string = [i for i in (''.join((ch if ch in '0123456789.-e' else ' ') for ch in length_string)).split()]
width_string = [i for i in (''.join((ch if ch in '0123456789.-e' else ' ') for ch in width_string)).split()]
height_string = [i for i in (''.join((ch if ch in '0123456789.-e' else ' ') for ch in height_string)).split()]
if 'e' in length_string:
    length_string.remove('e')
if 'e' in width_string:
    width_string.remove('e')
if 'e' in height_string:
    height_string.remove('e')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM