【发布时间】:2013-03-14 12:45:16
【问题描述】:
我目前正在使用 python(在 Ubuntu 上)开发一个可视化跟踪路由程序。我正在使用 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
最后打印显示错误“TypeError: 'int' object has no attribute 'getitem'”
【问题讨论】:
-
另外,您的循环可以简化:
holder_iter = (gi.record_by_addr(m) for m in matchob); listcoor = [holder for holder in holder_iter if holder is not None]或:listcoor = filter(None, (gi.record_by_addr(m) for m in matchob))如果gi.record_by_addr获得对象或无。
标签: python list dictionary geolocation