【发布时间】:2015-02-18 14:34:26
【问题描述】:
我正在尝试统计在包含我从 URL 中抓取的 800K+ 顶级域字符串的文件中出现的顶级域实例的数量。在下面的代码中,当我使用“if mstlds in ntld:”时,结果似乎是正确的,但经过检查,“co”和“com”、“ca”和“cat”的计数不正确。但是,如果我使用 == 或“is”,我根本不会得到任何匹配,而是会出现错误:
Traceback(最近一次调用最后一次): 文件“checktlds4malware.py”,第 111 行,在 mtlds_line = mtlds.readline() AttributeError: 'str' 对象没有属性 'readline'
tld_file = open(sys.argv[1],'r')
tld_line = tld_file.readline()
while tld_line:
#print(tld_line)
tld_line = tld_line.strip()
columns = tld_line.split()
ntld = columns[0] # get the ICANN TLD
ntld = ntld.lower()
mtlds = open ('malwaretlds.txt', 'r')
mtlds_line = mtlds.readline()
while mtlds_line:
print(mtlds_line)
mtlds_line = mtlds_line.strip()
columns = mtlds_line.split()
mtlds = columns[0]
mtlds = mtlds.lower()
#raw_input()
# I don't get the error when using "in" not ==
# but the comparison is not correct.
if mtlds_line == ntld:
m_count += 1
print 'ntld and mtld match: Malware domain count for ', ntld, m_count
mtlds_line = mtlds.readline()
print 'Final malware domain count for ', ntld, m_count
【问题讨论】:
-
mltds代表什么?当你执行mtlds = open ('malwaretlds.txt', 'r')时它指向一个文件对象,但在循环内你执行mtlds = columns[0],所以它指向一个字符串。应该是什么?文件还是字符串?你应该选择一种类型并坚持下去。 -
感谢您提供帮助。让我试着纠正一下。
-
是的,我应该为字符串使用不同的标签,如“mtld”,为文件对象使用 mtlds。现在可以用了,真的很欣赏眼球,看不到这个感觉有点傻。
标签: python attributeerror