blob: fff5965db153aa2f0f689d935ece09de381184dc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
'''
Task #1 › Valid Phone Numbers
Submitted by: Mohammad S Anwar
You are given a text file.
Write a script to display all valid phone numbers in the given text file.
Acceptable Phone Number Formats
+nn nnnnnnnnnn
(nn) nnnnnnnnnn
nnnn nnnnnnnnnn
Input File
0044 1148820341
+44 1148820341
44-11-4882-0341
(44) 1148820341
00 1148820341
Output
0044 1148820341
+44 1148820341
(44) 1148820341
'''
import re
import os
#Variables
text = """
0044 1148820341
+44 1148820341
44-11-4882-0341
(44) 1148820341
00 1148820341
"""
def askFileName():
file_name = input("Please, introduce the name of the file\n")
#We check that the file exists, if not, the file is created with a specific text
if (os.path.isfile(file_name) == False):
f = open(file_name, 'w')
f.write(str(text))
f.close()
print ("Output:")
readTelf(file_name)
def readTelf(file_name):
with open(file_name, 'r') as my_file:
for row in my_file:
if (re.match(r"\+\d{2}\s\d{10}", row)) or (re.match(r"\(\d{2}\)\s\d{10}", row)) or (re.match(r"\d{4}\s\d{10}", row)):
print(row,end="")
askFileName()
|