Python 在两个不同的标题下打印循环的输出

Python 在两个不同的标题下打印循环的输出,python,python-3.x,sorting,format,grouping,Python,Python 3.x,Sorting,Format,Grouping,我想尝试将输出格式化为在标题下列出 我制作了一个python(v3.6)脚本,它检查URL(包含在一个纺织品中)和输出是否安全或恶意 循环语句: """ This iterates through each url in the textfile and checks it against google's database. """ f = open("file.txt", "r") for weburl in f: sb = threat_matches_find(weburl

我想尝试将输出格式化为在标题下列出

我制作了一个python(v3.6)脚本,它检查URL(包含在一个纺织品中)和输出是否安全或恶意

循环语句:

"""
This iterates through each url in the textfile and checks it against google's database.
"""

f = open("file.txt", "r")

for weburl in f:

    sb = threat_matches_find(weburl) # Google API module

    if url_checker == {}:  # the '{}' represent = Safe URL
        print ("Safe :", url)

    else:
        print ("Malicious :", url)
由此得出的结果是:

>>python url\u checker.py
安全:url1.com
恶意:url2.com
安全:url3.com
恶意:url4.com
安全:url5.com
目标是获取要在标题(组)下列出/排序的url,如下所示:

如果url是安全的,请在“安全url”下打印url,否则为“恶意”

>>> python url_checker.py
Safe URLs:
url1.com
url3.com
url5.com

Malicious URLs:
url2.com
url4.com

我没有找到与我的问题相关的其他职位。任何帮助都将不胜感激。

您可以在循环时附加到列表,然后在填充两个列表时打印:

safe = []
malicious = []
for weburl in f:
    sb = threat_matches_find(weburl) # Google API module
    if url_checker == {}:  # the '{}' represent = Safe URL
        safe.append(url)
    else:
        malicious.append(url)

print('Safe URLs', *safe, '', sep='\n')
print('Malicious URLs', *malicious, '', sep='\n')
样本输出:

safe = ['url1.com','url3.com','url5.com']
malicious = ['url2.com','url4.com']

Safe URLs
url1.com
url3.com
url5.com

Malicious URLs
url2.com
url4.com

您可以在循环时附加到列表,然后在填充两个列表时打印:

safe = []
malicious = []
for weburl in f:
    sb = threat_matches_find(weburl) # Google API module
    if url_checker == {}:  # the '{}' represent = Safe URL
        safe.append(url)
    else:
        malicious.append(url)

print('Safe URLs', *safe, '', sep='\n')
print('Malicious URLs', *malicious, '', sep='\n')
样本输出:

safe = ['url1.com','url3.com','url5.com']
malicious = ['url2.com','url4.com']

Safe URLs
url1.com
url3.com
url5.com

Malicious URLs
url2.com
url4.com

那么什么是
url\u checker
?这与
sb
有什么关系?那么
url\u checker
是什么呢?这与
sb
有什么关系?
print('safeurl',*Safe,sep='\n')
会更干净。然后可以在末尾添加额外的换行符,使用
print('Safe url',*Safe',sep='\n')
print('Safe url',*Safe,sep='\n')
会更干净。然后,您可以使用
print('safeurls',*Safe',sep='\n')在末尾添加额外的换行符。