I am trying to use .join() and .format() together in the same loop, but unable to do so.
Sample Data:
JavaScript
x
2
1
somelist = [[{'web': 'www.example.com'}], [{'web': 'http://www.example.com/content/vb'}, {'123.123.3968 ': 'tel:123.123.3968'}], [{'web': 'https://example.com/fsdds/'}, {'': 'mailto:abc.xyz@ddaa.com?subject=Inquiry from abc.com%20'}], [{'web': 'https://example.com/svsd'}, {'web': 'http://example.com/vsdv'}, {'098.765.4321 ': 'tel:098.765.4321'}, {'B': 'mailto:xssc.xyz@ddaa.com?subject=Inquiry from xyz.com'}]]
2
Filtering out phone number as below:
JavaScript
1
7
1
phn = []
2
for lst in somelist:
3
ph_check = []
4
for ph in lst:
5
ph_check += [val for val in ph.values() if re.match(r'tel:(?(d{3}))?[^d]*(d{3})[^d]*(d{4})', val)]
6
phn.append(', '.join(ph_check) if ph_check else '')
7
I want to format the phone number in the below format, can anyone suggest me how to pass the below format:
JavaScript
1
2
1
"{} {} {}".format(ph_check.group(1), ph_check.group(2), ph_check.group(3))
2
Getting output as: tel:123.456.7890
But I want the output as: 123 456 7890
Advertisement
Answer
JavaScript
1
3
1
phone_nr = "tel:123.456.7890"
2
phone_nr[4:].replace('.', ' ')
3
Result:
JavaScript
1
2
1
123 456 7890
2
This makes:
JavaScript
1
11
11
1
import re
2
3
somelist = [[{'web': 'www.example.com'}], [{'web': 'http://www.example.com/content/vb'}, {'123.123.3968 ': 'tel:123.123.3968'}], [{'web': 'https://example.com/fsdds/'}], [{'web': 'https://example.com/svsd'}, {'web': 'http://example.com/vsdv'}, {'098.765.4321 ': 'tel:098.765.4321'}]]
4
5
phn = []
6
for lst in somelist:
7
ph_check = []
8
for ph in lst:
9
ph_check += [val for val in ph.values() if re.match(r'tel:(?(d{3}))?[^d]*(d{3})[^d]*(d{4})', val)]
10
phn.append(', '.join(ph_check)[4:].replace('.', ' ') if ph_check else '')
11