pulling from file in a certain format python -
i have 2 questions:
write program retrieves phone number format (nnn)nnn-nnnn in file.
make program retrieve every email address ending in .com present in file
they different want checking strings in file read. far can read file , put each line array. i'm not sure how disregard \n @ end.
code:
newfile = open('filepath') numfile = newfile.readlines() numbers = [] line in num: numbers.append(line) newfile.close print numbers
re module can used.
import re pattern1 = '\(\d{3}\)\d{3}-\d{4}' re.findall(pattern1, "my number (234)456-3678 , number (567)789-4567.") assuming file has same content mentioned in second parameter of findall. create pattern email also.
pattern2 = '[a-za-z0-9._%+-]+@[a-za-z0-9.-]+\.com' re.findall(pattern2, "my email abc@hotmail.com , email abcdef_23@gmail.com")
Comments
Post a Comment