Normalize Phone Numbers Using Python
I am extremely new to python. I often get text files that have phone numbers is various formats. I am trying to create a python script that takes this text file and normalizes them
Solution 1:
This might help you:
import re
withopen('test_numbers.txt') as f:
dirty = f.readlines()
clean = []
for l in dirty:
clean.apped('+1{},\n'.format(re.sub(r'[^0-9]', '', l)))
clean
will be a list of lines with +1
at the beginning and ,
at the end. You may then save it to a text file with:
withopen('formatted_numbers.txt', 'w') as f:
f.writelines(clean)
You can also use a one liner using list comprehension:
clean = ['+1{},\n'.format(re.sub(r'[^0-9]', '', l)) for l in dirty]
Post a Comment for "Normalize Phone Numbers Using Python"