如何在python中格式化电话号码
所以,我的电话号码格式化程序的实现非常糟糕。它应该采用电话号码格式的实例值,并以xxx.xxx.xxxx的格式返回它(目前它只应该是美国电话号码) 代码可在以下要点中找到:如何在python中格式化电话号码,python,string-formatting,phone-number,Python,String Formatting,Phone Number,所以,我的电话号码格式化程序的实现非常糟糕。它应该采用电话号码格式的实例值,并以xxx.xxx.xxxx的格式返回它(目前它只应该是美国电话号码) 代码可在以下要点中找到: 有很多方法可以做到这一点。函数的更简单版本是: def format_phone(self): # strip non-numeric characters phone = re.sub(r'\D', '', self.phone) # remove leading 1 (area codes nev
有很多方法可以做到这一点。函数的更简单版本是:
def format_phone(self):
# strip non-numeric characters
phone = re.sub(r'\D', '', self.phone)
# remove leading 1 (area codes never start with 1)
phone = phone.lstrip('1')
return '{}.{}.{}'.format(phone[0:3], phone[3:6], phone[6:])
有很多方法可以做到这一点。函数的更简单版本是:
def format_phone(self):
# strip non-numeric characters
phone = re.sub(r'\D', '', self.phone)
# remove leading 1 (area codes never start with 1)
phone = phone.lstrip('1')
return '{}.{}.{}'.format(phone[0:3], phone[3:6], phone[6:])