【问题标题】:Iterate over a list in python遍历python中的列表
【发布时间】:2014-10-10 09:53:26
【问题描述】:
我正在使用模块 pythonwhois 来获取特定域上的许多条目的列表:
def whois(self):
host = str(self.EntryText.get().lstrip("http://www."))
whois = pythonwhois.net.get_whois_raw(host)
print whois
whois
上面返回一个条目列表,如下所示:
[u"域名:google.com\n注册域 ID:\n注册商 WHOIS 服务器:whois.markmonitor.com\n注册商 URL:http://www.markmonitor.com\n更新日期:2014-05-19T04:00:17-0700\ n创建日期:1997-09-15T00:00:00-0700\n注册商注册到期日期:2020-09-13T21:00:00-0700\n
我的问题是:如何遍历列表并将结果打印到一个优雅的人类可读列表中?
【问题讨论】:
标签:
python
python-2.7
whois
【解决方案1】:
一个简单的方法是简单地打印返回的whois中的每个字符串:
host = 'stackoverflow.com'
whois = pythonwhois.net.get_whois_raw(host)
for item in whois:
print item
这将输出如下内容:
Domain Name: STACKOVERFLOW.COM
Registrar WHOIS Server: whois.name.com
Registrar URL: http://www.name.com
Updated Date: 2014-05-09T17:51:17-06:00
Creation Date: 2003-12-26T19:18:07-07:00
Registrar Registration Expiration Date: 2015-12-26T19:18:07-07:00
Registrar: Name.com, Inc.
Registrar IANA ID: 625
Registrar Abuse Contact Email: abuse@name.com
Registrar Abuse Contact Phone: +1.17202492374
Reseller:
Domain Status: clientTransferProhibited
Registrant Name: Sysadmin Team
Registrant Organization: Stack Exchange, Inc.
Registrant Street: 1 Exchange Plaza , Floor 26
Registrant City: New York
Registrant State/Province: NY
Registrant Postal Code: 10006
Registrant Country: US
etc.
任何比这更优雅的方法都需要您使用pythonwhois.get_whois(host),然后浏览返回的字典格式并显示感兴趣的字段。
【解决方案2】:
使用 pprint 为您完成。
from pprint import pprint
pprint(whois)