pythonopenrefinegrel

How to add numbers to a string with Python or GREL


I have >4000 numbers in a column that need to be manipulated.. They look like this: 040 413 560 89 or 0361 223240

How dow I put it into the folllowing format: +49 (040) 41356089 or +49 (0361) 223240

They all need to have the same country dialling code +49 and then the respective area code put into brackets and some are already in the correct format.


Solution

  • Do so like this:

    ls_alreadycorrected = ['(',')','+49']
    str_in = '040 413 560 89' #or apply to list
    
    for flag in ls_alreadycorrected:
        if flag not in str_in:
            how_many_spaces = str_in.count(' ')
            if how_many_spaces > 2:
                str_in = str_in.replace(' ','')
                str_out = '+049'+' ' + '(' + str_in[:3] + ') ' + str_in[-8:]
            else:
                str_in = str_in.replace(' ','')
                str_out = '+049'+' ' + '(' + str_in[:4] + ') ' + str_in[-6:]
    

    That's only given you have to types of phone numbers. For a list of numbers, put this on top instead of str_in

    for number in list_of_numbers:
        str_in = number
    

    Cheers