Python:libphonenumber无法提取明显的电话号码

2024-09-29 00:19:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Python版本的Google的libphonenumbers,但是当我在不同的文本上尝试这个库时,有时Python函数不会返回任何东西,而很明显那里有一个电话号码,有时它们确实返回电话号码。请看下面:

print(x2)
for match in pnum.PhoneNumberMatcher(x2, "US"):
print(match) #for the text above, it did not get the number

输出:

I just read your profile and thought it was really great. I also thought you were cute and loved the fact that you go hiking with your brothers every summer. If you want to know anything more about me, just ask.  My num 555-121-5468.

有了上面的这段文字,它不会返回我任何电话号码。 但在其他情况下,如以下情况,此函数会为我提供正确的输入:

x9 = "hay I hate to cut you short, its been fun chatting, but unfortuantely I gotta run. I am gald we became friends though. my number is (323) 2387890"

 for match in pnum.PhoneNumberMatcher(x9, "US"):
 print(match)

输出:

 PhoneNumberMatch [132,145) (323) 2387890

我不知道是什么问题导致了这个问题,我是Python和这个库的新手,非常感谢您的理解。你知道吗


Tags: the函数inyounumberformatch电话号码
1条回答
网友
1楼 · 发布于 2024-09-29 00:19:27

555-121-5468看起来像是一个有效的美国电话号码,但实际上不是

PhoneNumberMatcherconstructor接受leniency参数,该参数定义类与候选电话号码(code)匹配的严格程度。此参数的可能值leniency的默认值是1,它将只匹配有效的电话号码。将此更改为0将匹配可能的电话号码,如555-121-5468。你知道吗

>>> for match in pnum.PhoneNumberMatcher(x2, 'US', leniency=0): 
print(match) 
...
PhoneNumberMatch [220,232) 555-121-5468

555前缀不是真正的前缀,但在美国电视和电影中用于虚构的电话号码。从Wikipedia

Telephone numbers with the prefix 555 are widely used for fictitious telephone numbers in North American television shows, films, video games, and other media in order to prevent practical jokers and curious callers from bothering real people and organisations by telephoning numbers they see in works of fiction; generally, in North America, a number with 555 as a prefix will not connect to a real person.

相关问题 更多 >