Search code examples
pythonpython-3.xregexpython-re

How to remove space from number followed by unit or dimensions?


Here is the input string

string1 = 0.9% SODIUM CHLORIDE 8290306544 FLUSH 0.9 % SYRINGE 10 ML
string2 = 0.9% SODIUM CHLORIDE 8290-3071-44 FLUSH 0.9 % SYRINGE 10 MM
string3 = 0.9% SODIUM CHLORIDE 290306544 FLUSH 0.9 % SYRINGE 10 cm

These are three string that I'm working on, so here I want two remove space from number followed by unit/dimension/mesurments and % as well, eg- 10 ML => 10ML but 8290306544FLUSH this is wrong. and second thing is if there is 10 digit number then make format like 4 digit - 4 digit - 2 digit. eg- 8290-3065-44 and if there is 9 digit the add zero at first and make it in format. eg- 290306544 => 0290306544 => 0290-3065-44

I want output like

string1 = 0.9% SODIUM CHLORIDE 8290-3065-44 FLUSH 0.9% SYRINGE 10ML
string2 = 0.9% SODIUM CHLORIDE 8290-3071-44 FLUSH 0.9% SYRINGE 76MM
string3 = 0.9% SODIUM CHLORIDE 0290-3065-44 FLUSH 0.9% SYRINGE 65cm

how I make python function for this


Solution

  • This code may help you.

    # pip install quantities
    from quantities import units
    string1 ='0.9% SODIUM CHLORIDE 8290306544 FLUSH 0.9 % SYRINGE 10 ML'
    string2 = '0.9% SODIUM CHLORIDE 8290-3071-44 FLUSH 0.9 % SYRINGE 10 MM'
    string3 = '0.9% SODIUM CHLORIDE 290306544 FLUSH 0.9 % SYRINGE 10 cm'
    
    def string_formater(string):
        unit_symbols = [u.symbol for _, u in units.__dict__.items() if isinstance(u, type(units.deg))] # list of all units
    
        string = string.strip().split(' ') # strip remove unwanted spaces and split make a list.
     
    
        for a in string:
            if a.lower() in unit_symbols or a.upper() in unit_symbols: # if a is a unit then combine it with his previous value example '10','cm' then it becomes '10cm'.
                index = string.index(a)
                string[index-1] = string[index-1]+ string[index]
                del string[index]
    
        def number_formater(num):
            num = list(num)
            num.insert(4,'-')
            num.insert(9,'-')
            return(''.join(num)) # return the formated number with dash('-')
    
        for a in string:
            if a.isdigit():
                if len(a) == 9:
                    index = string.index(a)
                    a = '0'+a
                    string[index] = number_formater(a)
                elif len(a) == 10:
                    index = string.index(a)
                    string[index] = number_formater(a)
    
        return(' '.join(string))
    
    
    
    print(string_formater(string1)) # 0.9% SODIUM CHLORIDE 8290-3065-44 FLUSH 0.9% SYRINGE 10ML
    print(string_formater(string2)) # 0.9% SODIUM CHLORIDE 8290-3071-44 FLUSH 0.9% SYRINGE 76MM
    print(string_formater(string3)) # 0.9% SODIUM CHLORIDE 0290-3065-44 FLUSH 0.9% SYRINGE 65cm