0

私は現在、Python (Ubuntu 上) でビジュアル トレース ルート プログラムに取り組んでいます。私はpygeoipを使用して座標を取得しています(他の情報の中でも)。各 IP のデータをリスト (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 '」というエラーが表示されます。

4

1 に答える 1

3

にインデックスを付けようとして12いますが、整数にはインデックスを付けることができません。インデックス構文を移動して、 のインデックスの結果12インデックスを付けlistcoorます。

listcoor[12]['longitude']

何が起こっているかは次のとおりです。

>>> 12['longitude']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is unsubscriptable
于 2013-03-14T12:46:45.407 に答える