我正在尝试清理URL,并从中仅提取Id(跳过URL部分)。通过进行一些调试,我可以看到我正在寻找的值正在打印,但是没有返回(或没有返回)
代码如下:
def _sanitize_urls(urls=None):
redact_list = [
("abcd.google.com", 3),
("xyz.yahoo.com", 4),
]
urls_sanitized = []
redact_found = [redact for redact in redact_list if redact[0] in urls]
if redact_found:
urls = urls.split(" ")
print(urls)
urls_sanitized = [
words.split("/")[redact_found[0][1]] if redact_found[0][0] in words else words for words in urls
]
print(urls_sanitized)
urls_sanitized = " ".join(urls_sanitized)
print(urls_sanitized)
redact_found = [redact for redact in redact_list if redact[0] in urls_sanitized]
print(redact_found)
if not redact_found:
print(urls_sanitized)
return urls_sanitized
else:
_sanitize_urls(urls_sanitized)
def main():
urls = "https://abcd.google.com/ID-XXXX and https://xyz.yahoo.com/Id/ID-XXXX"
redact_exists = _sanitize_urls(urls)
print(redact_exists)
if __name__ == "__main__":
main()
我期望的输出为=>;“ID-XXXX和ID-XXXX”。我现在得到的输出是零
在我这边进行一些调试=>
['https://abcd.google.com/ID-XXXX', 'and', 'https://xyz.yahoo.com/Id/ID-XXXX']
['ID-XXXX', 'and', 'https://xyz.yahoo.com/Id/ID-XXXX']
ID-XXXX and https://xyz.yahoo.com/Id/ID-XXXX
[('xyz.yahoo.com', 4)]
['ID-XXXX', 'and', 'https://xyz.yahoo.com/Id/ID-XXXX']
['ID-XXXX', 'and', 'ID-XXXX']
ID-XXXX and ID-XXXX
[]
ID-XXXX and ID-XXXX
None
如您所见,print,print正确的值,直到最后一刻,但是它没有返回到main函数,而是没有返回任何值。有什么想法吗
在
_sanitize_urls
函数的最后一行,您有在此之后,您的函数就没有代码了。在Python中,如果函数在没有显式
return
语句的情况下耗尽了代码,那么None
将自动返回相关问题 更多 >
编程相关推荐