Skip to content

Instantly share code, notes, and snippets.

@cyb3rsalih
Created February 5, 2025 20:59
Show Gist options
  • Save cyb3rsalih/7f5140948311947ebee8b9206b0a50ab to your computer and use it in GitHub Desktop.
Save cyb3rsalih/7f5140948311947ebee8b9206b0a50ab to your computer and use it in GitHub Desktop.
Format Turkish Numbers when you have a list of entered phone numbers in various format. Use this to make all same.
import re
def format_phone_number(phone):
# Remove all non-digit characters
digits = re.sub(r'\D', '', phone)
# If number starts with 0, remove it
if digits.startswith('0'):
digits = digits[1:]
# If number doesn't start with 90, add it
if not digits.startswith('90'):
digits = '90' + digits
# Check if the final number has the correct length (12 digits)
if len(digits) == 12:
return digits
return None
def process_file(input_file, output_file):
formatted_numbers = set() # Using set to remove duplicates
with open(input_file, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if line: # Skip header lines
formatted = format_phone_number(line)
if formatted:
formatted_numbers.add(formatted)
# Sort and write unique formatted numbers
with open(output_file, 'w', encoding='utf-8') as f:
for number in sorted(formatted_numbers):
f.write(number + '\n')
return len(formatted_numbers)
# Process the file
input_file = 'numbers.txt'
output_file = 'formatted_numbers.txt'
count = process_file(input_file, output_file)
print(f"İşlem tamamlandı. {count} adet geçerli numara bulundu.")
# w/ Claude 3.5 Sonnet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment