Andy
Andy

Reputation: 541

Remove Decimal Numbers From String but Retain Whole Numbers in Python

I have a number of strings that contain a mixture of text, special characters, decimal numbers and whole numbers. Here are some examples:

Run Ho 1132  0.0,-0.5

Run Af 29  0.0

The whole numbers can in theory be any size but the decimal numbers will have a maximum of 3 digits either side of the decimal point.

I want to remove the decimal numbers along with any commas and - symbols but retain all whole numbers.

So in the above examples the desired output is:

Run Ho 1132

Run Af 29

Is this possible?

Upvotes: 0

Views: 327

Answers (1)

enzo
enzo

Reputation: 11486

You can do it by splitting, slicing and joining:

strings = ("Run Ho 1132  0.0,-0.5" ,"Run Af 29  0.0")
print([' '.join(string.split()[:3]) for string in strings])
# Outputs ['Run Ho 1132', 'Run Af 29']

If your input's format is not fixed, you may consider Regex:

import re
# Reads "0/1 whitespace followed by (1+ letters or 1+ numbers) followed by 1 whitespace"
# This will match a word and only integers, since a dot is not whitespace
pattern = re.compile(r'\s?(\w+|\d+)\s')

strings = ("Run Ho 1132  0.0,-0.5" ,"Run Af 29  0.0", "Run Pm 3.14  45")
replaced = []
for string in strings:
    match = pattern.findall(string)
    if match is None:
        continue
    replaced.append(' '.join(match))
    
print(replaced)
# Outputs ['Run Ho 1132', 'Run Af 29', 'Run Pm 45']

Upvotes: 2

Related Questions