I need to turn phonenumbers into international format. I have a list of phone number examples
rows = [
(datetime.time(20, 35, 30), '0707262078',),
(datetime.time(20, 38, 18), '+46706332602',),
(datetime.time(20, 56, 35), '065017063'),
(datetime.time(21, 45, 1), '+46730522807',),
(datetime.time(22, 13, 47), '0046733165812')
]
I need to replace all numbers starting with ex 07 with +467, all 06 with +466 and 00 with +. For above example I need the number to turn out 0707262078 to +46707262078, 065017063 to +4665017063 and 0046733165812 to +46733165812.
Dont know if its possible to do this in regex only or if I need to do it with other code.
Been trying with re.sub combined with lamda, my thought is to make a dictionary with the matching replaces like this:
repl_dict = {
'01': '+461',
'02': '+462',
'03': '+463',
'04': '+464',
'05': '+465',
'06': '+466',
'07': '+467',
'08': '+468',
'09': '+469',
'00': '+'
}
My try so far:
import re
rows = [
(datetime.time(20, 35, 30), '0707262078',),
(datetime.time(20, 38, 18), '+46706332602Ring via Mitel ',),
(datetime.time(20, 56, 35), '065017063'),
(datetime.time(21, 45, 1), '+46730522807Ring via Mitel ',),
(datetime.time(22, 13, 47), '0046733165812')
]
repl_dict = {
'01': '+461',
'02': '+462',
'03': '+463',
'04': '+464',
'05': '+465',
'06': '+466',
'07': '+467',
'08': '+468',
'09': '+469',
'00': '+'
}
for row in rows:
regex = re.compile(r'^\d{1}[0-9](\d*)'), re.S
DialedNumber = regex.sub(lambda match: repl_dict.get(match.group(0), row[1]), row[1], row[1])