我正在尝试清理URL,并从中仅提取ID(跳过URL部分)。通过进行一些调试,我可以看到我正在寻找的值是打印,但是不返回(或返回None)
这是代码:
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”。我现在得到的输出是None。
在我这边进行一些调试=>
['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
如您所见,打印,直到最后一刻都打印正确的值,但是它不会返回到主函数,而是返回无。有任何想法吗?
In the last line of your
_sanitize_urls
function, you haveThere's no more code to your function after this. In Python, if a function runs out of code without an explicit
return
statement,None
will be returned automatically.