Skip to content

Instantly share code, notes, and snippets.

@Vages
Created February 28, 2017 14:11
Show Gist options
  • Save Vages/0994872d1a4170e47400604a40c3dcfb to your computer and use it in GitHub Desktop.
Save Vages/0994872d1a4170e47400604a40c3dcfb to your computer and use it in GitHub Desktop.
Formats phone numbers
import os
filename = "mv17.csv"
phone_column = 2
with open(os.path.join(".", filename), "r") as f:
new_lines = []
for i, l in enumerate(f.readlines()):
if i == 0:
new_lines.append(l)
continue
elems = l.split(",")
phoneno = elems[phone_column].replace(" ", "")
if len(phoneno) == 8 and "+" not in phoneno:
phoneno = phoneno[:3] + " " + phoneno[3:5] + " " + phoneno[5:]
phoneno = "+47 " + phoneno
elems[phone_column] = phoneno
new_lines.append(",".join(elems))
with open(os.path.join(".", "new_" + filename), "w") as g:
g.writelines(new_lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment