Skip to content

Instantly share code, notes, and snippets.

@rudimuliawan
Created August 14, 2015 08:23
Show Gist options
  • Select an option

  • Save rudimuliawan/b1edabc45496cf815e94 to your computer and use it in GitHub Desktop.

Select an option

Save rudimuliawan/b1edabc45496cf815e94 to your computer and use it in GitHub Desktop.
import csv
import unicodecsv
from django.core.exceptions import ValidationError
from stamps.core.utils import normalize_phone
from stamps.core.validators import validate_mobile_phone
dirty_csv = '/home/rudi/Documents/seorae_dirty.csv'
clean_csv = open('/home/rudi/Documents/seorae_clean.csv', 'w')
with open(dirty_csv, 'rU') as csv_file:
csv_writer = unicodecsv.writer(clean_csv)
rows = csv.reader(csv_file)
for row in rows:
phone = row[0].strip().replace(' ', '').replace('-', '').decode('utf-8')
print phone
try:
_ = validate_mobile_phone(phone)
except ValidationError as e:
pass
else:
phone = normalize_phone(phone)
csv_writer.writerow([phone])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment