0
我目前正在使用python(在Ubuntu上)的Visual trace路徑程序。我正在使用pygeoip來獲取座標(以及其他信息)。我將每個IP的數據存儲在一個列表(listcoor)中。我遇到的問題是訪問一個特定的字典項一旦它被放置在listcoor訪問列表中的字典中的項目
def vargeolocating(matchob): # matchob is a list of IPs
print "Geolocating IP addresses"
gi = GeoIP.open("/usr/share/GeoIP/GeoLiteCity.dat",GeoIP.GEOIP_STANDARD)
i = 0
listcoor = []
while (i < len(matchob)):
holder = gi.record_by_addr(matchob[i])
if holder is None:# for local addresses
print "None"
else:
listcoor.append(holder)
i = i + 1
print holder['longitude'] # Prints out the last longitude
print listcoor[12] # Prints all information about the last IP (this longitude matchs the above longitude
print listcoor[12['longitude']] # Should print just the longitude, matching the two longitudes above
最後打印顯示錯誤「類型錯誤:‘詮釋’對象有沒有屬性‘的GetItem’」
另外,你的循環可以簡化爲:'holder_iter =(GI。 record_by_addr(m)for m in matchob);如果'gi.record_by_addr'獲得一個對象或None,'listcoor = filter(None,(gi.record_by_addr(m)for m in matchob)''listcoor = [持有者持有者持有者持有者。 – hughdbrown 2013-03-14 13:54:53